library("arm")
library("foreign")
library("car")
library("blme")
library("matrixStats")
setwd("~/Documents/Dropbox/Rader Lax/Vote Switching and Op Assg")
setwd("~/Dropbox/PoliticalScience/Rader Lax/Vote Switching and Op Assg")
#setwd("C:/documents and settings/jeffrey r. lax/my documents/my documents - #synced/rader lax/vote switching and op assg")
#setwd("C:\\Documents and Settings\\Jeffrey R. Lax\\My Documents\\My Documents -- Synced\\Rader Lax\\Vote Switching and Op Assg")
#setwd("/Users/Jeffrey/Documents/My Documents -- Synced/Rader Lax/Vote Switching and op assg")
#setwd("/Users/Jeffrey/Documents/Dropbox/Rader Lax/Vote Switching and Op Assg")


rm(list=ls(all=TRUE))
detach(ccoa)

ccoa <- read.dta("RL CCOA use.dta",convert.underscore = TRUE)
ccoa<-data.frame(ccoa)
ccoa$nyt<-(as.numeric(ccoa$nyt) - 1)
ccoa$laws<-(as.numeric(ccoa$laws) -1)
ccoa$opcount1 <- ccoa$opcount + 1
ccoa$assignor <- ifelse(ccoa$assignor=="Frankfur","Frankfurter", ccoa$assignor)
ccoa <- ccoa[order(ccoa$justice),]
ccoa$maj.coalition <- substr(tapply(ccoa$justice[ccoa$confM.justice==1], ccoa$us[ccoa$confM.justice==1], unique )[ccoa$us], 3,150)
ccoa$maj.coalition <-  gsub(")","",ccoa$maj.coalition)
ccoa$maj.coalition <-  gsub(",","",ccoa$maj.coalition)
ccoa$maj.coalition <-  gsub("\"","",ccoa$maj.coalition)
ccoa <- ccoa[order(ccoa$us),]
#above creates a variable containing list of majoriy coalition members
ccoa$natct.maj.coalition <- paste(ccoa$natct,ccoa$maj.coalition)
ccoa$assignorswitchvote <- ifelse(ccoa$justice==ccoa$assignor & ccoa$switch.rev==1, 1,0)
ccoa$assignorstuckinthiscase <-  1- tapply(ccoa$assignorswitchvote, ccoa$us, max)[ccoa$us]
ccoa$assignorstuckinthiscase <- ifelse(ccoa$assignor=="" |ccoa$assignor=="Stone" | ccoa$us=="353/0087" ,NA,ccoa$assignorstuckinthiscase)

ccoa$natct.maj.coalition.assignee <- paste(ccoa$natct.maj.coalition," assignee",ccoa$assignee, sep="")
detach(ccoa)

# 
# #create conservative coalition justice-level analysis (confD = 1 means liberal?)
# tempdata.con <- subset(ccoa, confM.justice ==1 & confD == 0  & conf.total.M < 9)
# tempdata.lib <- subset(ccoa, confM.justice ==1  & confD == 1 & conf.total.M < 9)
# 
# listofjustices <- unique(ccoa$justice)
# 



attach(ccoa) 

comparecoalitions <- tapply(switch.rev[confM.justice==1& assignorstuckinthiscase==1], list(natct.maj.coalition[confM.justice==1& assignorstuckinthiscase==1],assignee[confM.justice==1& assignorstuckinthiscase==1]),mean,na.rm=TRUE)

#write.csv(comparecoalitions, "comparecoalitions.csv")


listofcoals <- (table(natct.maj.coalition[assignorstuckinthiscase==1]))/9
#write.csv(listofcoals,"listofcoals.csv")


apply(comparecoalitions,1,max, na.rm=TRUE) - apply(comparecoalitions,1,min, na.rm=TRUE)


comparecoalitions <- read.csv("comparecoalitions.csv", row.names = 1, colClasses=c("factor",rep("numeric", 26)))

temp <- apply(comparecoalitions,1,max, na.rm=TRUE) - apply(comparecoalitions,1,min, na.rm=TRUE)

assignee.varies.by.coaltion <- (rowSums(!is.na(comparecoalitions)) > 1)
defection.occurrsinrow <- (apply(comparecoalitions.thatvary, 1,max,na.rm=TRUE) > 0)
comparecoalitions.thatvary <- subset(comparecoalitions, assignee.varies.by.coaltion==TRUE)

comparecoalitions.thatvary.andhavedefection <- subset(comparecoalitions.thatvary, defection.occurrsinrow )

rowSums(!is.na(comparecoalitions.thatvary)) 


apply(comparecoalitions.thatvary,1,max, na.rm=TRUE) - apply(comparecoalitions.thatvary,1,min, na.rm=TRUE)


hist(temp[temp>0])

cc.weightmatrix <-  (
tapply(switch.rev[confM.justice==1& assignorstuckinthiscase==1], list(natct.maj.coalition[confM.justice==1& assignorstuckinthiscase==1],assignee[confM.justice==1& assignorstuckinthiscase==1]),sum,na.rm=TRUE) +
tapply(1-switch.rev[confM.justice==1& assignorstuckinthiscase==1], list(natct.maj.coalition[confM.justice==1& assignorstuckinthiscase==1],assignee[confM.justice==1& assignorstuckinthiscase==1]),sum,na.rm=TRUE)  )[assignee.varies.by.coaltion==TRUE,]
cc.weightvector <- rowSums(cc.weightmatrix, na.rm=TRUE)


cc.variation.sd <- apply(comparecoalitions.thatvary,1,sd, na.rm=TRUE)
hist(cc.variation.sd)

weighted.mean(cc.variation.sd, cc.weightvector)

table(cc.variation.sd>0)
table(cc.variation.sd[defection.occurrsinrow]>0)


cc.variation.maxmindiff <- apply(comparecoalitions.thatvary,1,max, na.rm=TRUE) - apply(comparecoalitions.thatvary,1,min, na.rm=TRUE)
hist(cc.variation.maxmindiff)
table(cc.variation.maxmindiff>0)
table(cc.variation.maxmindiff[defection.occurrsinrow]>0)

weighted.mean(cc.variation.maxmindiff, cc.weightvector)
weighted.mean(cc.variation.maxmindiff)
table(cc.variation.sd>0)

weighted.mean(cc.variation.maxmindiff, cc.weightvector)

weighted.mean(cc.variation.maxmindiff[defection.occurrsinrow], cc.weightvector[defection.occurrsinrow])



#detach(ccoa)
#tempselect <- runif(length(ccoa[,1]))
#ccoa.select <- ccoa[tempselect<.50,]
#attach(ccoa.select) 

### Note: this data only includes analu=0
#IP.dist.assignee       MQ abs distance assignee to justice
#baum.dist.assignee     baum abs distance assignee to justice
#B.dist.assignee        bailey abs distance assignee to justice
#SC.dist.assignee       segal cover abs distance assignee to justice
#pl.dist.assignee       percent-liberal abs distance assignee to justice

#x_medside is median is more extreme than justice (toward conf side)
#x_dist_confmedian is signed distance to conf median 
#x_dist_confmedian2 is absolute distance to conf median.


n.sims <- 1000

combo <- paste(assignee,justice)
IP.assignee.relative.location <- ifelse(IP.side==0,-IP.dist.assignee, IP.dist.assignee)
pl.assignee.relative.location <- ifelse(pl.side==0,-pl.dist.assignee, pl.dist.assignee)
B.assignee.relative.location <- ifelse(B.side==0,-B.dist.assignee, B.dist.assignee)
SC.assignee.relative.location <- ifelse(SC.side==0,-SC.dist.assignee, SC.dist.assignee)
IP.notside <- 1- IP.side
IP.confmedside <- ifelse(IP.dist.confmedian>0,1,0) # means confmedian is more extreme than justice

IP.assignee.sign <- ifelse(IP.side==0, IP.signed - IP.dist.assignee, IP.signed + IP.dist.assignee)
B.assignee.sign <- ifelse(B.side==0, B.signed - B.dist.assignee, B.signed + B.dist.assignee)
pl.assignee.sign <- ifelse(pl.side==0, pl.signed - pl.dist.assignee, pl.signed + pl.dist.assignee)
SC.assignee.sign <- ifelse(SC.side==0, SC.signed - SC.dist.assignee, SC.signed + SC.dist.assignee)

IP.median.signcheck <-  ifelse(IP.medside==1, IP.signed + IP.dist.median2, IP.signed - IP.dist.median2)
IP.assignee.signcheck <- ifelse(confD==1, IP.assignee, -IP.assignee)
IP.confmedian.sign <- ifelse(IP.confmedside==1, IP.signed + IP.dist.median2, IP.signed - IP.dist.median2)




####################### Defection MLM (using MQ)

############# MAIN DEFECTION MODEL IN AJPS
# regular model (note, no side jump included)
mlm.maj.48 <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
+ uncert.justice + freshman  + IP.closer.min + IP.dist.assignee + IP.side:IP.dist.assignee + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"), data=ccoa)   
display(mlm.maj.48,detail=TRUE) 

sim.mlm.maj.48 <- sim(mlm.maj.48,n.sims) 
sim.mlm.maj.48.disteffectside1  <-(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"] + fixef(sim.mlm.maj.48)[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.disteffectside1)
rm(sim.mlm.maj.48) 
####################################### 

# comparing to blgmer
# regular model (note, no side jump included)
bmlm.maj.48 <- bglmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
                    + uncert.justice + freshman  + IP.closer.min + IP.dist.assignee + IP.side:IP.dist.assignee + 
                      (1|conf.total.M) 
                    + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"), data=ccoa)   
display(bmlm.maj.48,detail=TRUE)

### no difference except ran efs for groups slightly larger.  stick to regular.


########## now putting in interactions

# regular model (note, no side jump included)
mlm.maj.48.int <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal     + uncert.justice + freshman  + IP.closer.min + IP.dist.assignee + IP.side:IP.dist.assignee + 
                      (1|conf.total.M) 
                    + (1|value) + (1|justice) + (1|natct)
                    + assignee.justice:IP.marginal  + assignee.justice:uncert.justice + assignee.justice:freshman + assignee.justice:IP.closer.min
                    , subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"), data=ccoa)   
display(mlm.maj.48.int,detail=TRUE)






####################### MAIN CONFORMITY MODEL IN AJPS

mlm.min.48 <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig   + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M)  + (1|value) + (1|justice) + (1|natct), subset = confM.justice==0 & conf.total.M < 9, family = binomial(link="probit"), data=ccoa)  
display(mlm.min.48,detail=TRUE)
sim.mlm.min.48 <- sim(mlm.min.48,n.sims) 
sim.mlm.min.48.disteffectside1  <-(fixef(sim.mlm.min.48)[,"IP.dist.assignee"] + fixef(sim.mlm.min.48)[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.min.48.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.min.48.disteffectside1)
rm(sim.mlm.min.48) 
################################################



######################################  GRAPHS in AJPS
########### Main defection model coeffs 
####### Graph of mlm.maj.48

coef.maj.48<-c(fixef(mlm.maj.48)[2:13], mean(sim.mlm.maj.48.disteffectside1))
var.maj.48<-c("ideology","expertise","case salience (NYT)","more than one legal provision","noncontiguous majority","assigned","marginal","vote uncertain","freshman","closer to minority","distance to assignee\n(on moderate side)","side x distance\nto assignee","distance to assignee\n(on extreme side)")
se.maj.48<-c(se.fixef(mlm.maj.48)[2:13],sd(sim.mlm.maj.48.disteffectside1))

conf.95<-rep(qnorm(.975),length(coef.maj.48)) ##vector for confidence int. 

conf.90<-rep(qnorm(.95),length(coef.maj.48))

graph.min <- -.7
graph.max <- .7
graph.by <- .1

y.axis <- c(length(coef.maj.48):1)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis

pdf("CCOA_mlm_coeffs_fig1.pdf", height = 7, width = 6)  #open pdf device
par(mfrow=c(1,1))                       #grid for multiple graphs on page
par(mar=c(2, 8, 4, 1.5))                #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(coef.maj.48, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        #set limits of x-axis so that they include mins and maxs of coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals
title(main = "Probit Coefficients, Majority Model",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(coef.maj.48-conf.95*se.maj.48, y.axis, coef.maj.48+conf.95*se.maj.48, y.axis, lwd =  1.2)#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
segments(coef.maj.48-conf.90*se.maj.48, y.axis -.1, coef.maj.48-conf.90*se.maj.48, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
segments(coef.maj.48+conf.90*se.maj.48, y.axis -.1, coef.maj.48+conf.90*se.maj.48, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T,          #draw x-axis and labels with tick marks
   cex.axis = .4, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T, las  = 1,    #same as x-axis,but on top axis so
   line =0, cex.axis = .4, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = var.maj.48, las = 1, tick = T,
   cex.axis = .5)                                       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()
dev.off()


####### Predicted probabilities using MAIN DEFECTION MODEL: mlm.maj.48

### subset the data 

majjj <- data.frame(IP.signed,opcount1,nyt,laws,IP.noncontig,assignee.justice, IP.marginal, uncert.justice,freshman,IP.closer.min, IP.dist.assignee, IP.side, justice, natct, value, conf.total.M, switch.rev)

majj <- data.frame(subset(majjj, confM.justice==1 & conf.total.M < 9))

maj<- data.frame(na.omit(majj))

maj$value <- recode(as.numeric(maj$value), "12=11")


###Ideology similar/dissimilar to conference

ideo.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*(mean(maj$IP.signed)+sd(maj$IP.signed))
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
ideo.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*(mean(maj$IP.signed)-sd(maj$IP.signed))
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)

ideo.prob0 <-mean(ideo.0)
ideo.prob1 <- mean(ideo.1)
ideo.se0 <- sd(ideo.0)
ideo.se1 <- sd(ideo.1)

### Laws (more than one legal provision)

laws.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*0
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)

	
laws.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*1
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
laws.prob0 <- mean(laws.0)
laws.prob1 <- mean(laws.1)
laws.se0 <- sd(laws.0)
laws.se1 <- sd(laws.1) 

### Vote uncertain

uncert.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*0
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
uncert.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*1
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
uncert.prob0 <- mean(uncert.0)
uncert.prob1 <- mean(uncert.1)
uncert.se0 <- sd(uncert.0)
uncert.se1 <- sd(uncert.1) 

### Noncontig 

noncontig.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*0
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
noncontig.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*1
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
noncontig.prob0 <- mean(noncontig.0)
noncontig.prob1 <- mean(noncontig.1)
noncontig.se0 <- sd(noncontig.0)
noncontig.se1 <- sd(noncontig.1)


### Marginals

marg.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*0
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
marg.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*1
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
marg.prob0 <- mean(marg.0)
marg.prob1 <- mean(marg.1)
marg.se0 <- sd(marg.0)
marg.se1 <- sd(marg.1)

### Closer to min

close.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*0
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
close.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*maj$assignee.justice
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*1
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
close.prob0 <- mean(close.0)
close.prob1 <- mean(close.1)
close.se0 <- sd(close.0)
close.se1 <- sd(close.1)

### Assignee

assign.0 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*0
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
assign.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*1
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*maj$IP.dist.assignee
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(maj$IP.dist.assignee*maj$IP.side)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
assign.prob0 <- mean(assign.0)
assign.prob1 <- mean(assign.1)
assign.se0 <- sd(assign.0)
assign.se1 <- sd(assign.1)

### Moderate side: 1 and 10 MQ away

modside.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*0
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*1
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*0
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
modside.10 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*0
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*10
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*0
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
modside.prob1 <- mean(modside.1)
modside.prob10 <- mean(modside.10)
modside.se1 <- sd(modside.1)
modside.se10 <- sd(modside.10)

### Extreme Side side: 1 and 10 MQ away

exside.1 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*0
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*1
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(1*1)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[as.numeric(maj$value),1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
exside.10 <- pnorm(fixef(mlm.maj.48)["(Intercept)"]
	+ fixef(mlm.maj.48)["IP.signed"]*maj$IP.signed
	+ fixef(mlm.maj.48)["log(opcount1)"]*log(maj$opcount1)
    + fixef(mlm.maj.48)["nyt"]*maj$nyt
    + fixef(mlm.maj.48)["laws"]*maj$laws
    + fixef(mlm.maj.48)["IP.noncontig"]*maj$IP.noncontig
    + fixef(mlm.maj.48)["assignee.justice"]*0
    + fixef(mlm.maj.48)["IP.marginal"]*maj$IP.marginal
    + fixef(mlm.maj.48)["uncert.justice"]*maj$uncert.justice
    + fixef(mlm.maj.48)["freshman"]*maj$freshman
    + fixef(mlm.maj.48)["IP.closer.min"]*maj$IP.closer.min
    + fixef(mlm.maj.48)["IP.dist.assignee"]*10
    + fixef(mlm.maj.48)["IP.dist.assignee:IP.side"]*(10*1)
    + ranef(mlm.maj.48)$justice[maj$justice,1]
    + ranef(mlm.maj.48)$natct[maj$natct,1]
    + ranef(mlm.maj.48)$value[maj$value,1]
    + ranef(mlm.maj.48)$conf.total.M[maj$conf.total.M-3,1]
	)
	
exside.prob1 <- mean(exside.1)
exside.prob10 <- mean(exside.10)
exside.se1 <- sd(exside.1)
exside.se10 <- sd(exside.10)

####### Graphs of predicted probabilities using mlm.maj.48

prob <- c(ideo.prob0,ideo.prob1,NA,laws.prob0,laws.prob1,NA,uncert.prob0,uncert.prob1,NA,noncontig.prob0,noncontig.prob1,NA,assign.prob0,assign.prob1,NA,marg.prob0,marg.prob1,NA,close.prob0,close.prob1,NA,exside.prob10,exside.prob1,modside.prob1,modside.prob10)
prob.names<-c("ideology similar to conference decision","ideology dissimilar to conference decision","one legal provision","more than one legal provision","vote not uncertain","vote uncertain","contiguous","non-contiguous","not assigned","assigned","not marginal","marginal","closer to majority","closer to minority","extreme side: 10 MQ away", "extreme side: 1 MQ away","moderate side: 1 MQ away", "moderate side: 10 MQ away")
prob.se <- c(ideo.se0,ideo.se1,NA,laws.se0,laws.se1,NA,uncert.se0,uncert.se1,NA,noncontig.se0,noncontig.se1,NA,assign.se0,assign.se1,NA,marg.se0,marg.se1,NA,close.se0,close.se1,NA,exside.se10,exside.se1,modside.se1,modside.se10)

graph.min <- 0
graph.max <- .35
graph.by <- .05

y.axis <- c(length(prob):1)
y.axis2 <- c(25,24,22,21,19,18,16,15,13,12,10,9,7,6,4,3,2,1)
pdf("CCOA_pred_probs.pdf", height = 7, width = 6)   #open pdf device
par(mar=c(2, 9, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(prob, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        
title(main = "Majority Coalition Predicted Defection Probabilities",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(prob-.5*prob.se, y.axis, prob+.5*prob.se, y.axis, lwd =  1.1)#one-tailed test, lwd adjusts line thickness
#segments(prob-qnorm(.90)*prob.se, y.axis -.1, prob-qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)#one-tailed test
#segments(prob+qnorm(.90)*prob.se, y.axis -.1, prob+qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis2, label = prob.names, las = 1, cex.axis = .5)       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis2, lty = 3, col=2)

box()
dev.off()


####### Graph of prob switch over range of IP.dist.assignee using mlm.maj.48

graph.types <- rbind(rep(1,24),
rep(mean(maj$IP.signed),24),
rep(mean(log(maj$opcount1)),24),
rep(mean(maj$nyt),24),
rep(mean(maj$laws),24),
rep(mean(maj$IP.noncontig),24),
rep(0,24),      #assignee=0
rep(mean(maj$IP.marginal),24),
rep(mean(maj$uncert.justice),24),
rep(mean(maj$freshman),24),
rep(mean(maj$IP.closer.min),24),
c(11:0,0:11),   #distance 
c(rep(0,12),0:11) #side*distance
) 

graph.data <- pnorm((fixef(sim.mlm.maj.48))%*%graph.types)
graph.points <- rep(NA,ncol(graph.data))
graph.se <- rep(NA,ncol(graph.data))
for (i in 1:ncol(graph.data)){
    graph.points[i] <- mean(graph.data[,i])
    graph.se[i] <- sd(graph.data[,i])
    }

graph.min <- -11
graph.max <- 11
graph.by <- 2

x.axis <- c(-11:0,0:11)
		
pdf("CCOA_distance.pdf", height = 7, width = 6) #open pdf device
par(mar=c(4, 4, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
layout(matrix(c(1,1,1,2,2), 1, 5, byrow = TRUE))
plot(x.axis, graph.points, type = "n", axes = F, xlab = "Location of Assignee Relative to Justice", ylab = "Probability of Defection", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.2), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.20,by=.05), labels = seq(0,.2,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)    
text(x=c(-6,6),y=c(.008,.008),c('moderate side','extreme side'),cex=.6)                     
box()

####### Graph of prob switch 2

graph.min <- 0
graph.max <- 11
graph.by <- 2

x.axis <- c(11:0,0:11)

plot(x.axis, graph.points, type = "n", axes = F, xlab = "Distance to Assignee", ylab = "", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.2), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.20,by=.05), labels = seq(0,.2,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                            
text(x=c(3),y=c(.115),c('extreme side'),adj=0,cex=.6)
text(x=c(6),y=c(.062),c('moderate side'),adj=1,cex=.6)                      
box()
dev.off()



################ DEFECTION ROBUSTNESS CHECKS

# regular model (note, no side jump included)
mlm.maj.48.chief <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & (assignor=="Vinson" | assignor=="Burger"| assignor=="Warren"), family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.chief,detail=TRUE) 
sim.mlm.maj.48.chief <- sim(mlm.maj.48.chief,n.sims) 
sim.mlm.maj.48.chief.disteffectside1  <-(sim.mlm.maj.48.chief$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.chief$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.chief.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.chief.disteffectside1)
rm(sim.mlm.maj.48.chief) 


# regular model (note, no side jump included)
mlm.maj.48.salient <- glmer(formula = switch.rev ~ IP.signed + log(opcount1)  + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & nyt=="salient", family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.salient,detail=TRUE) 
sim.mlm.maj.48.salient <- sim(mlm.maj.48.salient,n.sims) 
sim.mlm.maj.48.salient.disteffectside1  <-(sim.mlm.maj.48.salient$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.salient$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.salient.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.salient.disteffectside1)
rm(sim.mlm.maj.48.salient) 


mlm.maj.48.justice<- glmer(formula = switch.rev ~ + IP.signed  
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justice,detail=TRUE) 

mlm.maj.48.justiceandassignee <- glmer(formula = switch.rev ~ + IP.signed  + IP.assignee.sign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandassignee,detail=TRUE) 
#    AIC = 11730.9, DIC = 11698.9

mlm.maj.48.justiceandmedian <- glmer(formula = switch.rev ~ + IP.signed  + IP.medsign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandmedian,detail=TRUE) 

mlm.maj.48.justiceandassigneeandmedian <- glmer(formula = switch.rev ~ + IP.signed  + IP.assignee.sign + IP.medsign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandassigneeandmedian,detail=TRUE) 


mlm.maj.48.justiceandconfmedian <- glmer(formula = switch.rev ~ + IP.signed  + IP.confmedian.sign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandconfmedian,detail=TRUE) 

mlm.maj.48.justiceandassigneeandconfmedian <- glmer(formula = switch.rev ~ + IP.signed  + IP.assignee.sign  + IP.confmedian.sign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandassigneeandconfmedian,detail=TRUE) 


mlm.maj.48.justiceandmedianandconfmedian <- glmer(formula = switch.rev ~ + IP.signed   + IP.medsign + IP.confmedian.sign
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandmedianandconfmedian,detail=TRUE) 

mlm.maj.48.justiceandmedianandconfmedianandassignee <- glmer(formula = switch.rev ~ + IP.signed   + IP.medsign + IP.confmedian.sign+ IP.assignee.sign 
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandmedianandconfmedianandassignee,detail=TRUE) 
display(mlm.maj.48.justiceandmedianandconfmedianandassigne,detail=TRUE) 


mlm.maj.48t <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee +  
(1|conf.total.M) + freshman + (1+IP.dist.assignee  + IP.side:IP.dist.assignee|justice)
+ (1|value)  + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48t,detail=TRUE) 


#mlm.maj.48.int <- lmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig 
#    + assignee.justice + IP.marginal + uncert.justice + freshman  
#    + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + assignee.justice:IP.marginal 
#    + assignee.justice:uncert.justice + assignee.justice:freshman + assignee.justice:IP.closer.min 
#    + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), 
#    subset = confM.justice==1 & conf.total.M < 9, family = binomial (link="probit"))
#display(mlm.maj.48.int,detail=TRUE)



mlm.min.48.medianadded <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig   + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M)  + (1|value) + (1|justice) + (1|natct)
+IP.dist.median2 + IP.medside:IP.dist.median2
, subset = confM.justice==0 & conf.total.M < 9, family = binomial(link="probit"))  
display(mlm.min.48.medianadded,detail=TRUE)

mlm.min.48.medianadded.contig <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws   + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M)  + (1|value) + (1|justice) + (1|natct)
+IP.dist.median2 + IP.medside:IP.dist.median2
, subset = confM.justice==0 & conf.total.M < 9  &IP.noncontig==0 , family = binomial(link="probit"))  
display(mlm.min.48.medianadded.contig,detail=TRUE)


mlm.maj.48.sophomore <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman2  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.sophomore,detail=TRUE) 
sim.mlm.maj.48.sophomore <- sim(mlm.maj.48.sophomore,n.sims) 
sim.mlm.maj.48.sophomore.disteffectside1  <-(sim.mlm.maj.48.sophomore$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.sophomore$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.sophomore.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.sophomore.disteffectside1)
rm(sim.mlm.maj.48.sophomore) 

# with median
mlm.maj.48.confmedian <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.confmedian2 + IP.closer.min + IP.confmedside:IP.dist.confmedian2 + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.confmedian,detail=TRUE) 
sim.mlm.maj.48.confmedian <- sim(mlm.maj.48.confmedian,n.sims) 
sim.mlm.maj.48.confmedian.disteffectmedside1  <-(sim.mlm.maj.48.confmedian$fixef[,"IP.dist.confmedian2"] + sim.mlm.maj.48.confmedian$fixef[,"IP.dist.confmedian2:IP.confmedside"]) 
quantile(sim.mlm.maj.48.confmedian.disteffectmedside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.confmedian.disteffectmedside1)
rm(sim.mlm.maj.48.confmedian) 

# with median
mlm.maj.48.confmedianadded <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.confmedian2 + IP.closer.min + IP.confmedside:IP.dist.confmedian2 + IP.dist.assignee + IP.side:IP.dist.assignee + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.confmedianadded,detail=TRUE) 
sim.mlm.maj.48.confmedianadded <- sim(mlm.maj.48.confmedianadded,n.sims) 
sim.mlm.maj.48.confmedianadded.disteffectside1  <-(sim.mlm.maj.48.confmedianadded$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.confmedianadded$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.confmedianadded.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.confmedianadded.disteffectside1)
sim.mlm.maj.48.confmedianadded.disteffectmedside1  <-(sim.mlm.maj.48.confmedianadded$fixef[,"IP.dist.confmedian2"] + sim.mlm.maj.48.confmedianadded$fixef[,"IP.dist.confmedian2:IP.confmedside"]) 
quantile(sim.mlm.maj.48.confmedianadded.disteffectmedside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.confmedianadded.disteffectmedside1)
rm(sim.mlm.maj.48.confmedianadded) 

mlm.maj.48.median <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.median2 + IP.closer.min + IP.medside:IP.dist.median2 + 
(1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.median,detail=TRUE) 
sim.mlm.maj.48.median <- sim(mlm.maj.48.median,n.sims) 
sim.mlm.maj.48.median.disteffectmedside1  <-(sim.mlm.maj.48.median$fixef[,"IP.dist.median2"] + sim.mlm.maj.48.median$fixef[,"IP.dist.median2:IP.medside"]) 
quantile(sim.mlm.maj.48.median.disteffectmedside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.median.disteffectmedside1)
rm(sim.mlm.maj.48.median) 

# with median
mlm.maj.48.medianadded <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.median2 + IP.closer.min + IP.medside:IP.dist.median2 + IP.dist.assignee + IP.side:IP.dist.assignee + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.medianadded,detail=TRUE) 
sim.mlm.maj.48.medianadded <- sim(mlm.maj.48.medianadded,n.sims) 
sim.mlm.maj.48.medianadded.disteffectside1  <-(sim.mlm.maj.48.medianadded$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.medianadded$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.maj.48.medianadded.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.medianadded.disteffectside1)
sim.mlm.maj.48.medianadded.disteffectmedside1  <-(sim.mlm.maj.48.medianadded$fixef[,"IP.dist.median2"] + sim.mlm.maj.48.medianadded$fixef[,"IP.dist.median2:IP.medside"]) 
quantile(sim.mlm.maj.48.medianadded.disteffectmedside1,c(.5,.95,.975)) 
sd(sim.mlm.maj.48.medianadded.disteffectmedside1)
rm(sim.mlm.maj.48.medianadded) 


#what if assume v-shape from justice?
mlm.maj.48.vshape <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.vshape,detail=TRUE)


#what if assume linear?
mlm.maj.48.linear <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig 
+ assignee.justice + IP.marginal + uncert.justice + freshman  + IP.assignee.signed + IP.closer.min  
+ (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, 
family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.linear,detail=TRUE)

#back to regular model, contig only
mlm.maj.48.contig <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws  + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & IP.noncontig==0, family = binomial(link="probit"))
display(mlm.maj.48.contig,detail=TRUE)
sim.mlm.maj.48.contig <- sim(mlm.maj.48.contig,n.sims)
sim.mlm.maj.48.contig.disteffectside1  <-(sim.mlm.maj.48.contig$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.contig$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.contig.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.contig.disteffectside1)
rm(sim.mlm.maj.48.contig)

mlm.maj.48.normal <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws  + assignee.justice + IP.marginal + uncert.justice + freshman + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & IP.normcontig==1, family = binomial(link="probit"))
display(mlm.maj.48.normal,detail=TRUE)
sim.mlm.maj.48.normal <- sim(mlm.maj.48.normal,n.sims)
sim.mlm.maj.48.normal.disteffectside1  <-(sim.mlm.maj.48.normal$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.normal$fixef[,"IP.side:IP.dist.assignee"])
quantile(sim.mlm.maj.48.normal.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.normal.disteffectside1)
rm(sim.mlm.maj.48.normal)

mlm.maj.48.normal.linear <- glmer(formula = switch.rev ~ IP.signed + IP.assignee.sign + log(opcount1) + nyt + laws  + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & IP.normcontig==1, family = binomial(link="probit"))
display(mlm.maj.48.normal.linear,detail=TRUE)



#what if assume v-shape from justice, contig only
mlm.maj.48.vshape.contig <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws  + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & IP.noncontig==0, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.vshape.contig,detail=TRUE)


#add namici
mlm.maj.48.amici <- glmer(formula = switch.rev ~ log(namici+1) + IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.amici,detail=TRUE)
sim.mlm.maj.48.amici <- sim(mlm.maj.48.amici,n.sims)
sim.mlm.maj.48.amici.disteffectside1  <-(sim.mlm.maj.48.amici$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.amici$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.amici.disteffectside1,c(.5,.975))
sd(sim.mlm.maj.48.amici.disteffectside1)
rm(sim.mlm.maj.48.amici)


#contig amici
mlm.maj.48.contig.amici <- glmer(formula = switch.rev ~ log(namici+1) + IP.signed + log(opcount1) + nyt + laws  + assignee.justice + IP.marginal + uncert.justice + freshman + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & IP.noncontig==0, family = binomial(link="probit"))  #+ IP.side 
display(mlm.maj.48.contig.amici,detail=TRUE)
sim.mlm.maj.48.contig.amici <- sim(mlm.maj.48.contig.amici,n.sims)
sim.mlm.maj.48.contig.amici.disteffectside1  <-(sim.mlm.maj.48.contig.amici$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.contig.amici$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.contig.amici.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.contig.amici.disteffectside1)
rm(sim.mlm.maj.48.contig.amici)

######  regular model only justice and conftotoal random effects
# regular model (note, no side jump included)
mlm.maj.48.simplemlm <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M)  + (1|justice) , subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.simplemlm,detail=TRUE)
sim.mlm.maj.48.simplemlm <- sim(mlm.maj.48.simplemlm,n.sims)
sim.mlm.maj.48.simplemlm.disteffectside1  <-(sim.mlm.maj.48.simplemlm$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.simplemlm$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.simplemlm.disteffectside1,c(.5,.95,.975))

######  regular model no ran effs
mlm.maj.48.simpleprobit <- glm(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee  , subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))
display(mlm.maj.48.simpleprobit,detail=TRUE)
sim.mlm.maj.48.simpleprobit <- sim(mlm.maj.48.simpleprobit,n.sims)
sim.mlm.maj.48.simpleprobit.disteffectside1  <-(sim.mlm.maj.48.simpleprobit$coef[,"IP.dist.assignee"] + sim.mlm.maj.48.simpleprobit$coef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.simpleprobit.disteffectside1,c(.5,.95,.975))



# regular model  but assignor doesn't switch
mlm.maj.48.assignorsticks <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
                          + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
                          , subset = confM.justice==1 & conf.total.M < 9 & assignorstuckinthiscase==1, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.assignorsticks,detail=TRUE)


#model with just ranefs

mlm.maj.48.manyre <- glmer(formula = switch.rev ~ 
#  + assignee.justice 
                           + (1|justice)  +(1|natct.maj.coalition) + (1|us) + (1|natct.maj.coalition.assignee)
                          , subset = confM.justice==1 & conf.total.M < 9 & assignorstuckinthiscase==1, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.manyre,detail=TRUE)

# regular model  but more complex
mlm.maj.48.combo <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
+ uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
+(1|combo) +(1|assignee)
, subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.combo,detail=TRUE)
sim.mlm.maj.48.combo <- sim(mlm.maj.48.combo,n.sims)
sim.mlm.maj.48.combo.disteffectside1  <-(sim.mlm.maj.48.combo$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.combo$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.combo.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.combo.disteffectside1)
rm(sim.mlm.maj.48.combo)

# regular model  but more complex
mlm.maj.48.coalition <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
                          + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
                          +(1|maj.coalition) +(1|assignee)
                          , subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.coalition,detail=TRUE)


mlm.maj.48.assignor <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal 
+ uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
 +(1|assignee)
, subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.assignor,detail=TRUE)
sim.mlm.maj.48.assignor <- sim(mlm.maj.48.assignor,n.sims)
sim.mlm.maj.48.assignor.disteffectside1  <-(sim.mlm.maj.48.assignor$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.assignor$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.assignor.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.assignor.disteffectside1)
rm(sim.mlm.maj.48.assignor)

# regular model  but more complex
mlm.maj.48.combo.contig <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
+(1|combo) +(1|assignee)
, subset = confM.justice==1 & conf.total.M < 9 & IP.noncontig==0, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.combo.contig,detail=TRUE)
sim.mlm.maj.48.combo.contig <- sim(mlm.maj.48.combo.contig,n.sims)
sim.mlm.maj.48.combo.contig.disteffectside1  <-(sim.mlm.maj.48.combo.contig$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.combo.contig$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.combo.contig.disteffectside1,c(.5,.95,.975))
rm(sim.mlm.maj.48.combo.contig)


mlm.maj.48.combo2 <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
 +(1|assignee) +(1|us)
, subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.combo2,detail=TRUE)
sim.mlm.maj.48.combo2 <- sim(mlm.maj.48.combo2,n.sims/4)
sim.mlm.maj.48.combo2.disteffectside1  <-(sim.mlm.maj.48.combo2$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.combo2$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.combo2.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.combo2.disteffectside1 )
rm(sim.mlm.maj.48.combo2)


mlm.maj.48.combo2b <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct) 
 +(1|assignee) + (1|combo) +(1|us)
, subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.combo2b,detail=TRUE)
sim.mlm.maj.48.combo2b <- sim(mlm.maj.48.combo2b,n.sims/4)
sim.mlm.maj.48.combo2b.disteffectside1  <-(sim.mlm.maj.48.combo2b$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.combo2b$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.combo2b.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.combo2b.disteffectside1 )
rm(sim.mlm.maj.48.combo2b)

mlm.maj.48.combo3 <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + as.factor(conf.total.M) + (value) + (justice) + (natct) 
 +(assignee) +(1|us)
, subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.combo3,detail=TRUE)
sim.mlm.maj.48.combo3 <- sim(mlm.maj.48.combo3,n.sims/2)
sim.mlm.maj.48.combo3.disteffectside1  <-(sim.mlm.maj.48.combo3$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.combo3$fixef[,"IP.dist.assignee:IP.side"])
quantile(sim.mlm.maj.48.combo3.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.combo3.disteffectside1)
rm(sim.mlm.maj.48.combo3)


####################### Defection MLM (using Segal Cover)


# regular model (note, no side jump included)
mlm.maj.48.SegCov <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws + SC.noncontig + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov,detail=TRUE)
sim.mlm.maj.48.SegCov <- sim(mlm.maj.48.SegCov,n.sims)
sim.mlm.maj.48.SegCov.disteffectside1  <-(sim.mlm.maj.48.SegCov$fixef[,"SC.dist.assignee"] + sim.mlm.maj.48.SegCov$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.disteffectside1 )
rm(sim.mlm.maj.48.SegCov)


#what if assume v-shape from justice?
mlm.maj.48.SegCov.vshape <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws + SC.noncontig + assignee.justice 
+ SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min 
+ (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.vshape,detail=TRUE)


#what if assume linear?
mlm.maj.48.SegCov.linear <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt 
+ laws + SC.noncontig + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.assignee.sign + SC.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.linear,detail=TRUE)




#back to regular model, contig only
mlm.maj.48.SegCov.contig <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws  + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & SC.noncontig==0, family = binomial(link="probit"))
display(mlm.maj.48.SegCov.contig,detail=TRUE)
sim.mlm.maj.48.SegCov.contig <- sim(mlm.maj.48.SegCov.contig,n.sims)
sim.mlm.maj.48.SegCov.contig.disteffectside1  <-(sim.mlm.maj.48.SegCov.contig$fixef[,"SC.dist.assignee"]
 + sim.mlm.maj.48.SegCov.contig$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.contig.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.contig.disteffectside1)
rm(sim.mlm.maj.48.SegCov.contig)

mlm.maj.48.SegCov.normal <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws  + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & SC.normcontig==1, family = binomial(link="probit"))
display(mlm.maj.48.SegCov.normal,detail=TRUE)
sim.mlm.maj.48.SegCov.normal <- sim(mlm.maj.48.SegCov.normal,n.sims)
sim.mlm.maj.48.SegCov.normal.disteffectside1  <-(sim.mlm.maj.48.SegCov.normal$fixef[,"SC.dist.assignee"]
 + sim.mlm.maj.48.SegCov.normal$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.normal.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.normal.disteffectside1)
rm(sim.mlm.maj.48.SegCov.normal)




#what if assume v-shape from justice, contig only
mlm.maj.48.SegCov.vshape.contig <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws  + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & SC.noncontig==0, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.vshape.contig,detail=TRUE)


#add namici
mlm.maj.48.SegCov.amici <- glmer(formula = switch.rev ~ log(namici+1) + SC.signed + log(opcount1) + nyt + laws + SC.noncontig
 + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.amici,detail=TRUE)
sim.mlm.maj.48.SegCov.amici <- sim(mlm.maj.48.SegCov.amici,n.sims)
sim.mlm.maj.48.SegCov.amici.disteffectside1  <-(sim.mlm.maj.48.SegCov.amici$fixef[,"SC.dist.assignee"] + sim.mlm.maj.48.SegCov.amici$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.amici.disteffectside1,c(.5,.975))
sd(sim.mlm.maj.48.SegCov.amici.disteffectside1)
rm(sim.mlm.maj.48.SegCov.amici)


#contig amici
mlm.maj.48.SegCov.contig.amici <- glmer(formula = switch.rev ~ log(namici+1) + SC.signed + log(opcount1) 
+ nyt + laws  + assignee.justice + SC.marginal + uncert.justice + freshman + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & SC.noncontig==0, family = binomial(link="probit"))  #+ SC.side 
display(mlm.maj.48.SegCov.contig.amici,detail=TRUE)
sim.mlm.maj.48.SegCov.contig.amici <- sim(mlm.maj.48.SegCov.contig.amici,n.sims)
sim.mlm.maj.48.SegCov.contig.amici.disteffectside1  <-(sim.mlm.maj.48.SegCov.contig.amici$fixef[,"SC.dist.assignee"] 
    + sim.mlm.maj.48.SegCov.contig.amici$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.contig.amici.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.contig.amici.disteffectside1)
rm(sim.mlm.maj.48.SegCov.contig.amici)


mlm.maj.48.SegCov.combo <- glmer(formula = switch.rev ~ SC.signed +(1|combo) +(1|assignee) + log(opcount1) + nyt + laws + SC.noncontig + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.combo,detail=TRUE)
sim.mlm.maj.48.SegCov.combo <- sim(mlm.maj.48.SegCov.combo,n.sims)
sim.mlm.maj.48.SegCov.combo.disteffectside1  <-(sim.mlm.maj.48.SegCov.combo$fixef[,"SC.dist.assignee"] + sim.mlm.maj.48.SegCov.combo$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.combo.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.combo.disteffectside1)
rm(sim.mlm.maj.48.SegCov.combo)

mlm.maj.48.SegCov.combo2 <- glmer(formula = switch.rev ~ SC.signed +(1|combo) +(1|assignee) + (1|us) + log(opcount1) + nyt + laws + SC.noncontig + assignee.justice + SC.marginal + uncert.justice + freshman  + SC.dist.assignee + SC.closer.min + SC.side:SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ SC.side
display(mlm.maj.48.SegCov.combo2,detail=TRUE)
sim.mlm.maj.48.SegCov.combo2 <- sim(mlm.maj.48.SegCov.combo2,n.sims)
sim.mlm.maj.48.SegCov.combo2.disteffectside1  <-(sim.mlm.maj.48.SegCov.combo2$fixef[,"SC.dist.assignee"] 
+ sim.mlm.maj.48.SegCov.combo2$fixef[,"SC.dist.assignee:SC.side"])
quantile(sim.mlm.maj.48.SegCov.combo2.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.SegCov.combo2.disteffectside1)
rm(sim.mlm.maj.48.SegCov.combo2)


####################### Defection MLM (using Bailey)


# regular model (note, no side jump included)
mlm.maj.48.Bailey <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + B.noncontig     + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min         + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct),         subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey,detail=TRUE)
sim.mlm.maj.48.Bailey <- sim(mlm.maj.48.Bailey,n.sims)
sim.mlm.maj.48.Bailey.disteffectside1  <-(sim.mlm.maj.48.Bailey$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.disteffectside1 )
rm(sim.mlm.maj.48.Bailey)


#what if assume v-shape from justice?
mlm.maj.48.Bailey.vshape <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.vshape,detail=TRUE)


#what if assume linear?
mlm.maj.48.Bailey.linear <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.assignee.sign  + B.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.linear,detail=TRUE)

mlm.maj.48.Bailey.linear.normal <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws  + assignee.justice + B.marginal + uncert.justice + freshman  + B.assignee.sign  + B.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & B.normcontig==1, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.linear.normal,detail=TRUE)


#back to regular model, contig only
mlm.maj.48.Bailey.contig <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws  + assignee.justice + B.marginal + uncert.justice + freshman + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & B.noncontig==0, family = binomial(link="probit"))
display(mlm.maj.48.Bailey.contig,detail=TRUE)
sim.mlm.maj.48.Bailey.contig <- sim(mlm.maj.48.Bailey.contig,n.sims)
sim.mlm.maj.48.Bailey.contig.disteffectside1  <-(sim.mlm.maj.48.Bailey.contig$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey.contig$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.contig.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.contig.disteffectside1)
rm(sim.mlm.maj.48.Bailey.contig)


mlm.maj.48.Bailey.normal <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws  + assignee.justice + B.marginal + uncert.justice + freshman + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & B.normcontig==1, family = binomial(link="probit"))
display(mlm.maj.48.Bailey.normal,detail=TRUE)
sim.mlm.maj.48.Bailey.normal <- sim(mlm.maj.48.Bailey.normal,n.sims)
sim.mlm.maj.48.Bailey.normal.disteffectside1  <-(sim.mlm.maj.48.Bailey.normal$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey.normal$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.normal.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.normal.disteffectside1)
rm(sim.mlm.maj.48.Bailey.normal)


#what if assume v-shape from justice, contig only
mlm.maj.48.Bailey.vshape.contig <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws  + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & B.noncontig==0, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.vshape.contig,detail=TRUE)


#add namici
mlm.maj.48.Bailey.amici <- glmer(formula = switch.rev ~ log(namici+1) + B.signed + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.amici,detail=TRUE)
sim.mlm.maj.48.Bailey.amici <- sim(mlm.maj.48.Bailey.amici,n.sims)
sim.mlm.maj.48.Bailey.amici.disteffectside1  <-(sim.mlm.maj.48.Bailey.amici$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey.amici$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.amici.disteffectside1,c(.5,.975))
sd(sim.mlm.maj.48.Bailey.amici.disteffectside1)
rm(sim.mlm.maj.48.Bailey.amici)


#contig amici
mlm.maj.48.Bailey.contig.amici <- glmer(formula = switch.rev ~ log(namici+1) + B.signed + log(opcount1) + nyt + laws  + assignee.justice + B.marginal + uncert.justice + freshman + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & B.noncontig==0, family = binomial(link="probit"))  #+ B.side 
display(mlm.maj.48.Bailey.contig.amici,detail=TRUE)
sim.mlm.maj.48.Bailey.contig.amici <- sim(mlm.maj.48.Bailey.contig.amici,n.sims)
sim.mlm.maj.48.Bailey.contig.amici.disteffectside1  <-(sim.mlm.maj.48.Bailey.contig.amici$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey.contig.amici$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.contig.amici.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.contig.amici.disteffectside1)
rm(sim.mlm.maj.48.Bailey.contig.amici)

mlm.maj.48.Bailey.combo <- glmer(formula = switch.rev ~ B.signed +(1|combo) +(1|assignee) + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.combo,detail=TRUE)
sim.mlm.maj.48.Bailey.combo <- sim(mlm.maj.48.Bailey.combo,n.sims)
sim.mlm.maj.48.Bailey.combo.disteffectside1  <-(sim.mlm.maj.48.Bailey.combo$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.Bailey.combo$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.combo.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.combo.disteffectside1)
rm(sim.mlm.maj.48.Bailey.combo)

mlm.maj.48.Bailey.combo2 <- glmer(formula = switch.rev ~ B.signed +(1|combo) +(1|assignee) + (1|us) + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ B.side
display(mlm.maj.48.Bailey.combo2,detail=TRUE)
sim.mlm.maj.48.Bailey.combo2 <- sim(mlm.maj.48.Bailey.combo2,n.sims)
sim.mlm.maj.48.Bailey.combo2.disteffectside1  <-(sim.mlm.maj.48.Bailey.combo2$fixef[,"B.dist.assignee"] 
+ sim.mlm.maj.48.Bailey.combo2$fixef[,"B.dist.assignee:B.side"])
quantile(sim.mlm.maj.48.Bailey.combo2.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.Bailey.combo2.disteffectside1)
rm(sim.mlm.maj.48.Bailey.combo2)






####################### Defection MLM (using Percent Liberal)


# regular model (note, no side jump included)
mlm.maj.48.PerLib <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws + pl.noncontig + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib,detail=TRUE)
sim.mlm.maj.48.PerLib <- sim(mlm.maj.48.PerLib,n.sims)
sim.mlm.maj.48.PerLib.disteffectside1  <-(sim.mlm.maj.48.PerLib$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.disteffectside1)
rm(sim.mlm.maj.48.PerLib)

+(1|combo) +(1|assignee)

mlm.maj.48.PerLib.combo <- glmer(formula = switch.rev ~ pl.signed +(1|combo) +(1|assignee) + log(opcount1) + nyt + laws + pl.noncontig + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.combo,detail=TRUE)
sim.mlm.maj.48.PerLib.combo <- sim(mlm.maj.48.PerLib.combo,n.sims)
sim.mlm.maj.48.PerLib.combo.disteffectside1  <-(sim.mlm.maj.48.PerLib.combo$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.combo$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.combo.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.combo.disteffectside1)
rm(sim.mlm.maj.48.PerLib.combo)

mlm.maj.48.PerLib.combo2 <- glmer(formula = switch.rev ~ pl.signed +(1|combo) +(1|assignee) + (1|us) + log(opcount1) + nyt + laws + pl.noncontig + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.combo2,detail=TRUE)
sim.mlm.maj.48.PerLib.combo2 <- sim(mlm.maj.48.PerLib.combo2,n.sims)
sim.mlm.maj.48.PerLib.combo2.disteffectside1  <-(sim.mlm.maj.48.PerLib.combo2$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.combo2$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.combo2.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.combo2.disteffectside1)
rm(sim.mlm.maj.48.PerLib.combo2)


#what if assume v-shape from justice?
mlm.maj.48.PerLib.vshape <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws + pl.noncontig 
+ assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.vshape,detail=TRUE)


#what if assume linear?
mlm.maj.48.PerLib.linear <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws 
+ pl.noncontig + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.assignee.sign 
+ pl.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.linear,detail=TRUE)




#back to regular model, contig only
mlm.maj.48.PerLib.contig <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws  + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & pl.noncontig==0, family = binomial(link="probit"))
display(mlm.maj.48.PerLib.contig,detail=TRUE)
sim.mlm.maj.48.PerLib.contig <- sim(mlm.maj.48.PerLib.contig,n.sims)
sim.mlm.maj.48.PerLib.contig.disteffectside1  <-(sim.mlm.maj.48.PerLib.contig$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.contig$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.contig.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.contig.disteffectside1 )
rm(sim.mlm.maj.48.PerLib.contig)

mlm.maj.48.PerLib.normal <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws  + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & pl.normcontig==1, family = binomial(link="probit"))
display(mlm.maj.48.PerLib.normal,detail=TRUE)
sim.mlm.maj.48.PerLib.normal <- sim(mlm.maj.48.PerLib.normal,n.sims)
sim.mlm.maj.48.PerLib.normal.disteffectside1  <-(sim.mlm.maj.48.PerLib.normal$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.normal$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.normal.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.normal.disteffectside1 )
rm(sim.mlm.maj.48.PerLib.normal)


#what if assume v-shape from justice, contig only
mlm.maj.48.PerLib.vshape.contig <- glmer(formula = switch.rev ~ pl.signed + log(opcount1) + nyt + laws  + assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & pl.noncontig==0, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.vshape.contig,detail=TRUE)


#add namici
mlm.maj.48.PerLib.amici <- glmer(formula = switch.rev ~ log(namici+1) + pl.signed + log(opcount1) + nyt + laws + pl.noncontig 
+ assignee.justice + pl.marginal + uncert.justice + freshman  + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ pl.side
display(mlm.maj.48.PerLib.amici,detail=TRUE)
sim.mlm.maj.48.PerLib.amici <- sim(mlm.maj.48.PerLib.amici,n.sims)
sim.mlm.maj.48.PerLib.amici.disteffectside1  <-(sim.mlm.maj.48.PerLib.amici$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.amici$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.amici.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.amici.disteffectside1 )
rm(sim.mlm.maj.48.PerLib.amici)


#contig amici
mlm.maj.48.PerLib.contig.amici <- glmer(formula = switch.rev ~ log(namici+1) + pl.signed + log(opcount1) + nyt + laws  + assignee.justice + pl.marginal + uncert.justice + freshman + pl.dist.assignee + pl.closer.min + pl.side:pl.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & pl.noncontig==0, family = binomial(link="probit"))  #+ pl.side 
display(mlm.maj.48.PerLib.contig.amici,detail=TRUE)
sim.mlm.maj.48.PerLib.contig.amici <- sim(mlm.maj.48.PerLib.contig.amici,n.sims)
sim.mlm.maj.48.PerLib.contig.amici.disteffectside1  <-(sim.mlm.maj.48.PerLib.contig.amici$fixef[,"pl.dist.assignee"] + sim.mlm.maj.48.PerLib.contig.amici$fixef[,"pl.dist.assignee:pl.side"])
quantile(sim.mlm.maj.48.PerLib.contig.amici.disteffectside1,c(.5,.95,.975))
sd(sim.mlm.maj.48.PerLib.contig.amici.disteffectside1 )
rm(sim.mlm.maj.48.PerLib.contig.amici)



####################### Defection MLM (using Baum)


# regular model (note, no side jump included)
mlm.maj.48.Baum <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + baum.side:baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ baum.side
display(mlm.maj.48.Baum,detail=TRUE)
sim.mlm.maj.48.Baum <- sim(mlm.maj.48.Baum,n.sims)
sim.mlm.maj.48.baum.disteffectside1  <-(sim.mlm.maj.48.Baum$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.Baum$fixef[,"baum.dist.assignee:baum.side"])
quantile(sim.mlm.maj.48.baum.disteffectside1,c(.5,.95,.975))
rm(sim.mlm.maj.48.Baum)


#what if assume v-shape from justice?
mlm.maj.48.baum.vshape <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ baum.side
display(mlm.maj.48.baum.vshape,detail=TRUE)


#what if assume linear?
mlm.maj.48.baum.linear <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.assignee + baum.closer.min  + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ baum.side
display(mlm.maj.48.baum.linear,detail=TRUE)




#back to regular model, contig only
mlm.maj.48.baum.contig <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws  + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + baum.side:baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & baum.noncontig==0, family = binomial(link="probit"))
display(mlm.maj.48.baum.contig,detail=TRUE)
sim.mlm.maj.48.baum.contig <- sim(mlm.maj.48.baum.contig,n.sims)
sim.mlm.maj.48.baum.contig.disteffectside1  <-(sim.mlm.maj.48.baum.contig$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum.contig$fixef[,"baum.side:baum.dist.assignee"])
quantile(sim.mlm.maj.48.baum.contig.disteffectside1,c(.5,.95,.975))
rm(sim.mlm.maj.48.Baum.contig)


#what if assume v-shape from justice, contig only
mlm.maj.48.baum.vshape.contig <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws  + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & baum.noncontig==0, family = binomial(link="probit"))   #+ baum.side
display(mlm.maj.48.baum.vshape.contig,detail=TRUE)


#add namici
mlm.maj.48.baum.amici <- glmer(formula = switch.rev ~ log(namici+1) + baum.signed + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + baum.side:baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ baum.side
display(mlm.maj.48.baum.amici,detail=TRUE)
sim.mlm.maj.48.baum.amici <- sim(mlm.maj.48.baum.amici,n.sims)
sim.mlm.maj.48.baum.amici.disteffectside1  <-(sim.mlm.maj.48.baum.amici$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum.amici$fixef[,"baum.dist.assignee:baum.side"])
quantile(sim.mlm.maj.48.baum.amici.disteffectside1,c(.5,.975))
rm(sim.mlm.maj.48.Baum.amici)


#contig amici
mlm.maj.48.baum.contig.amici <- glmer(formula = switch.rev ~ log(namici+1) + baum.signed + log(opcount1) + nyt + laws  + assignee.justice + baum.marginal + uncert.justice + freshman + baum.dist.assignee + baum.closer.min + baum.side:baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9 & baum.noncontig==0, family = binomial(link="probit"))  #+ baum.side 
display(mlm.maj.48.baum.contig.amici,detail=TRUE)
sim.mlm.maj.48.baum.contig.amici <- sim(mlm.maj.48.baum.contig.amici,n.sims)
sim.mlm.maj.48.baum.contig.amici.disteffectside1  <-(sim.mlm.maj.48.baum.contig.amici$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum.contig.amici$fixef[,"baum.dist.assignee:baum.side"])
quantile(sim.mlm.maj.48.baum.contig.amici.disteffectside1,c(.5,.95,.975))
rm(sim.mlm.maj.48.Baum.contig.amici)






#random.deviance <- rep(NA, 500)
#random.AIC <- rep(NA,500)
#random.coef <- rep(NA, 500)
#random.se <- rep(NA, 500)

for(i in 56:60){
pickajustice <- runif(length(ccoa[,1]))
pickajustice <- ifelse(confM.justice==1, pickajustice,-1)
picked.justice <- ifelse(pickajustice==tapply(pickajustice, us, max,na.rm=TRUE)[us], 1,0)
range(tapply(picked.justice,us,sum),na.rm=TRUE)
tapply(picked.justice,us,max)
IP.random.signed.temp <- ifelse(picked.justice==1,IP.signed, -1000)
IP.random.signed <- tapply(IP.random.signed.temp, us, max,na.rm=TRUE)[us]
IP.random.signed <- ifelse(IP.random.signed==-1000, NA, IP.random.signed)
mlm.maj.48.justiceandrandom <- glmer(formula = switch.rev ~ + IP.signed  + IP.random.signed
+ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.maj.48.justiceandrandom,detail=TRUE) 
#random.deviance[i] <- deviance(mlm.maj.48.justiceandrandom)
random.AIC[i] <- summary(mlm.maj.48.justiceandrandom)@AICtab["AIC"]
random.coef[i] <- fixef(mlm.maj.48.justiceandrandom)["IP.random.signed"]
random.se[i] <- se.fixef(mlm.maj.48.justiceandrandom)["IP.random.signed"]
}
print(random.AIC[is.na(random.AIC)==FALSE])
#comparison:#    AIC = 11730.9, DIC = 11698.9
mean(random.AIC < 11730.9, na.rm=TRUE)
#mean(random.coef > fixef(mlm.maj.48.justiceandassignee)["IP.assignee.sign"], na.rm=TRUE)
#mean(random.se < se.fixef(mlm.maj.48.justiceandassignee)["IP.assignee.sign"], na.rm=TRUE)
random.t <- random.coef / random.se
mean(random.t < -5.15, na.rm=TRUE)





#########  CONFORMITY ROBUSTNESS CHECKS

mlm.min.48.justiceandassignee <- glmer(formula = switch.rev ~ + IP.signed  + IP.assignee.sign
+ log(opcount1) + nyt + laws + IP.noncontig   + uncert.justice + freshman   + IP.closer.min 
 + (1|conf.total.M) 
+ (1|value) + (1|justice) + (1|natct), subset = confM.justice==0 & conf.total.M < 9, family = binomial(link="probit"))   #+ IP.side
display(mlm.min.48.justiceandassignee,detail=TRUE) 


mlm.min.48.contig <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws   + uncert.justice + freshman  + IP.dist.assignee + IP.closer.min + IP.side:IP.dist.assignee + 
(1|conf.total.M)  + (1|value) + (1|justice) + (1|natct), subset = confM.justice==0 & conf.total.M < 9 & IP.noncontig==0, family = binomial(link="probit"))  
display(mlm.min.48.contig,detail=TRUE)
sim.mlm.min.48.contig <- sim(mlm.min.48.contig,n.sims) 
sim.mlm.min.48.contig.disteffectside1  <-(sim.mlm.min.48.contig$fixef[,"IP.dist.assignee"] + sim.mlm.min.48.contig$fixef[,"IP.dist.assignee:IP.side"]) 
quantile(sim.mlm.min.48.contig.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.min.48.contig.disteffectside1)
rm(sim.mlm.min.48.contig) 



mlm.min.48.normal <- glmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws   + uncert.justice + freshman   + IP.closer.min + IP.dist.assignee + 
(1|conf.total.M)  + (1|value) + (1|justice) + (1|natct), subset = confM.justice==0 & conf.total.M < 9 & IP.normcontig==1, family = binomial(link="probit"))  
display(mlm.min.48.normal,detail=TRUE)
sim.mlm.min.48.normal <- sim(mlm.min.48.normal,n.sims) 
sim.mlm.min.48.normal.disteffectside1  <-(sim.mlm.min.48.normal$fixef[,"IP.dist.assignee"] ) 
quantile(sim.mlm.min.48.normal.disteffectside1,c(.5,.95,.975)) 
sd(sim.mlm.min.48.normal.disteffectside1)
rm(sim.mlm.min.48.normal) 



############################   OLDER STUFF

### Linear Combinations
n.sims <- 1000
sim.mlm.maj.48 <- sim(mlm.maj.48,n.sims)
### Treatment effect of distance, holding side contant at 1
mm1<-mean(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"] + fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])
msd1<-sd(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"] + fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])
### Change in effect of distance going from extreme to moderate (is effect of distance different when side=0 verses side=1?)
mm2<-mean(abs(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"]+fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])-abs(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"]))
msd2<-sd(abs(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"]+fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])-abs(fixef(sim.mlm.maj.48)[,"IP.dist.assignee"]))
### Change in from 1 MQ away side=0 to 1MQ away side=1
mm3<-mean(fixef(sim.mlm.maj.48)[,"IP.side"] +  fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])
msd3<-sd(fixef(sim.mlm.maj.48)[,"IP.side"] +  fixef(sim.mlm.maj.48)[,"IP.side:IP.dist.assignee"])

########## Bailey scores
mlm.maj.48.b <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman  + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))  #+ B.side
display(mlm.maj.48.b,detail=TRUE)

mlm.maj.48.b.contig <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman + B.dist.assignee + B.closer.min + B.side:B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9& IP.noncontig==0, family = binomial(link="probit"))   # + B.side
display(mlm.maj.48.b.contig,detail=TRUE)


### Linear Combinations for Bailey scores
n.sims <- 1000
sim.mlm.maj.48.b <- sim(mlm.maj.48.b,n.sims)
### Treatment effect of distance, holding side contant
mm1.b<-mean(sim.mlm.maj.48.b$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b$fixef[,"B.side:B.dist.assignee"])
msd1.b<-sd(sim.mlm.maj.48.b$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b$fixef[,"B.side:B.dist.assiddddgnee"])
mm1.b<-mean(sim.mlm.maj.48.b$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b$fixef[,"B.dist.assignee:B.side"])
msd1.b<-sd(sim.mlm.maj.48.b$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b$fixef[,"B.dist.assignee:B.side"])



### Linear Combinations for Bailey scores -- contig only
n.sims <- 1000
sim.mlm.maj.48.b.contig <- sim(mlm.maj.48.b.contig,n.sims)
### Treatment effect of distance, holding side contant
mm1.b.contig<-mean(sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b.contig$fixef[,"B.side:B.dist.assignee"])
msd1.b.contig<-sd(sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b.contig$fixef[,"B.side:B.dist.assignee"])
mm1.b.contig <-mean(sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee:B.side"])
msd1.b.contig<-sd(sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b.contig$fixef[,"B.dist.assignee:B.side"])


########## Bailey scores with namici
mlm.maj.48.b2 <- glmer(formula = switch.rev ~ B.signed + log(opcount1) + nyt + laws + log(namici+1) + B.noncontig + assignee.justice + B.marginal + uncert.justice + freshman + B.side + B.dist.assignee + B.closer.min + B.side*B.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))
display(mlm.maj.48.b2,detail=TRUE)

### Linear Combinations for Bailey scores with namici
n.sims <- 1000
sim.mlm.maj.48.b2 <- sim(mlm.maj.48.b2,n.sims)
### Treatment effect of distance, holding side contant
mm1.b2<-mean(sim.mlm.maj.48.b2$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b2$fixef[,"B.side:B.dist.assignee"])
msd1.b2<-sd(sim.mlm.maj.48.b2$fixef[,"B.dist.assignee"] + sim.mlm.maj.48.b2$fixef[,"B.side:B.dist.assignee"])

########## SC scores
mlm.maj.48.sc <- glmer(formula = switch.rev ~ SC.signed + log(opcount1) + nyt + laws + SC.noncontig + assignee.justice + SC.marginal + uncert.justice + freshman + SC.side + SC.dist.assignee + SC.closer.min + SC.side*SC.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))
display(mlm.maj.48.sc,detail=TRUE)

########## baum scores
mlm.maj.48.baum <- glmer(formula = switch.rev ~ baum.signed + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman  + baum.dist.assignee + baum.closer.min + baum.side:baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))  #+ baum.side
display(mlm.maj.48.baum,detail=TRUE)


### Linear Combinations for baum scores
n.sims <- 1000
sim.mlm.maj.48.baum <- sim(mlm.maj.48.baum,n.sims)
### Treatment effect of distance, holding side contant
mm1.baum <-mean(sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum$fixef[,"baum.side:baum.dist.assignee"])
msd1.baum<-sd(sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum$fixef[,"baum.side:baum.dist.assignee"])
mm1.baum <-mean(sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee:baum.side"])
msd1.baum<-sd(sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee"] + sim.mlm.maj.48.baum$fixef[,"baum.dist.assignee:baum.side"])



baumtest <- glmer(formula = switch.rev ~ baum.signed + baum.dist.median + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman + baum.side + baum.dist.assignee + baum.closer.min + baum.side*baum.dist.assignee + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==1 & conf.total.M < 9, family = binomial(link="probit"))
display(baumtest,detail=TRUE)














mlm.maj.48.int <- lmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig 
    + assignee.justice + IP.marginal + uncert.justice + freshman + IP.side 
    + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + assignee.justice*IP.marginal 
    + assignee.justice*uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min 
    + (1|conf.total.M) + (1|value) + (1|justice) + (1|natct), 
    subset = confM.justice==1 & conf.total.M < 9, family = binomial (link="probit"))
display(mlm.maj.48.int,detail=TRUE)

    


### Linear Combinations
sim.mlm.maj.48.int <- sim(mlm.maj.48.int,n.sims)
### Treatment effect of distance, holding side contant
mmi1<-mean(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])
msdi1<-sd(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"] + sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])
### Change in effect of distance going from extreme to moderate (is effect of distance different when side=0 verses side=1?)
mmi2<-mean(abs(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"]+sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])-abs(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"]))
msdi2<-sd(abs(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"]+sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])-abs(sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"]))
### Change in from 1 MQ away side=0 to 1MQ away side=1
mmi9<-mean(sim.mlm.maj.48.int$fixef[,"IP.side"] +  sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])
msdi9<-sd(sim.mlm.maj.48.int$fixef[,"IP.side"] +  sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])
### Treatment effect of assignment to marginal vs. foreign twin
mmi3<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"])
msdi3<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"])
### Treatment effect of assignment to uncertaun vs. foreign twin
mmi4<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:uncert.justice"])
msdi4<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:uncert.justice"])
### Treatment effect of assignment to freshman vs. foreign twin
mmi5<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:freshman"])
msdi5<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:freshman"])
### Treatment effect of assignment to closer.min vs. foreign twin
mmi6<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.closer.min"])
msdi6<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.closer.min"])
### Treatment effect of assignment to closer.min AND marginal vs. foreign twin
mmi7<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.closer.min"])
msdi7<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.closer.min"])
### For marginals, assignment to self instead of other justice who is "1" MQ unit away
mmi8<-mean(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"] - sim.mlm.maj.48.int$fixef[,"IP.side"] - sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"] - sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])
msdi8<-sd(sim.mlm.maj.48.int$fixef[,"assignee.justice"] + sim.mlm.maj.48.int$fixef[,"assignee.justice:IP.marginal"] - sim.mlm.maj.48.int$fixef[,"IP.side"] - sim.mlm.maj.48.int$fixef[,"IP.dist.assignee"] - sim.mlm.maj.48.int$fixef[,"IP.side:IP.dist.assignee"])

####################### Conforming MLM (using MQ)

mlm.min.48 <- lmer(formula = switch.rev ~ IP.signed + log(opcount1) + nyt + laws + IP.noncontig + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + as.factor(conf.total.M) + (1|value) + (1|justice) + (1|natct), subset = confM.justice==0 & conf.total.M < 9, family = binomial (link="probit"), method="Laplace", control=list(usePQL=TRUE))

### Linear Combinations
sim.mlm.min.48<- sim(mlm.min.48,n.sims)
### Treatment effect of distance, holding side contant
minm1<-mean(fixef(sim.mlm.min.48)[,"IP.dist.assignee"] + fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])
minsd1<-sd(fixef(sim.mlm.min.48)[,"IP.dist.assignee"] + fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])
### Change in effect of distance going from extreme to moderate (is effect of distance different when side=0 verses side=1?)
minm2<-mean(abs(fixef(sim.mlm.min.48)[,"IP.dist.assignee"]+fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])-abs(fixef(sim.mlm.min.48)[,"IP.dist.assignee"]))
minsd2<-sd(abs(fixef(sim.mlm.min.48)[,"IP.dist.assignee"]+fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])-abs(fixef(sim.mlm.min.48)[,"IP.dist.assignee"]))
### Change in from 1 MQ away side=0 to 1MQ away side=1
minm3<-mean(fixef(sim.mlm.min.48)[,"IP.side"] +  fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])
minsd3<-sd(fixef(sim.mlm.min.48)[,"IP.side"] +  fixef(sim.mlm.min.48)[,"IP.side:IP.dist.assignee"])


######################### Graphs 



####### Graph of mlm.maj.48.int

coef1.maj.48.int<-fixef(mlm.maj.48.int)
coef.maj.48.int<-c(coef1.maj.48.int[2:18],mmi1,mmi2,mmi9,mmi3,mmi4,mmi5,mmi6,mmi7,mmi8)
var.maj.48.int<-c("extremeness","expertise","case salience","case complexity","noncontiguous majority","assigned","marginal","vote uncertain","freshman","side (at 0 MQ distance)","distance to assignee\n(on moderate side)","closer to minority","side x distance to assignee","assigned x marginal","assigned x vote uncertain","assigned x freshman", "assigned x closer to minority","distance to assignee\n(on extreme side)","change in effect of distance,\nfrom moderate to extreme side","1 MQ away on moderate side vs.\n1 MQ away on extreme side","marginal assigned","vote uncertain assigned","freshman assigned","closer to minority assigned","marginal and closer to minority assigned","marginal when assignment is 1 MQ away")
se1.maj.48.int<-se.fixef(mlm.maj.48.int)
se.maj.48.int<-c(se1.maj.48.int[2:18],msdi1,msdi2,msdi9,msdi3,msdi4,msdi5,msdi6,msdi7,msdi8)


conf.95<-c(qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.975),qnorm(.95),qnorm(.975),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.975),qnorm(.975),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95)) ##vector fpr confidence int. Some tests are 2 tailed, some are one-tailed

conf.90<-c(qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.95),qnorm(.90),qnorm(.95),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.95),qnorm(.95),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90))  ##vector fpr confidence int. Some tests are 2 tailed, some are one-tailed

graph.min <- -.7
graph.max <- .7
graph.by <- .1


y.axis <- c(length(coef.maj.48.int):1)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis
pdf("CCOA_mlm_coeffs_fig2.pdf", height = 7, width = 6)  #open pdf device
par(mar=c(2, 9, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(coef.maj.48.int, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        #set limits of x-axis so that they include mins and maxs of coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals
title(main = "Probit Coefficients, Assignment Interaction Model",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(coef.maj.48.int-conf.95*se.maj.48.int, y.axis, coef.maj.48.int+conf.95*se.maj.48.int, y.axis, lwd = 1.2)#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
segments(coef.maj.48.int-conf.90*se.maj.48.int, y.axis -.1, coef.maj.48.int-conf.90*se.maj.48.int, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
segments(coef.maj.48.int+conf.90*se.maj.48.int, y.axis -.1, coef.maj.48.int+conf.90*se.maj.48.int, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T,          #draw x-axis and labels with tick marks
   cex.axis = .4, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T, las  = 1,    #same as x-axis,but on top axis so
   line =0, cex.axis = .4, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = var.maj.48.int, las = 1, tick = T,
   cex.axis = .5)                                       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)
abline(h=9.5)
#box (bty = "o") #place box around plot
#text(1.2, 5, expression(R^{2} == .15), adj = 0, cex = .7) #add text for R-squared
#text(1.2, 4, expression(paste("Adjusted ", R^{2} == .12, "")), adj = 0, cex= .7)#add text for Adjusted-R-squared
#text(1.2, 3, "n = 500", adj = 0, cex = .7)#add text for sample size
#segments(1.1, 2.7, 1.1,5.3)#use 3 segment commands to draw box around text; connect to  right-side of box
#segments(1.1, 2.7, 5,2.7)
#segments(1.1, 5.3, 5.2,5.3)
box()
dev.off()

####### Graph of mlm.min.48

coef1.min.48<-fixef(mlm.min.48)
coef.min.48<-c(coef1.min.48[2:11],coef1.min.48[16],minm1,minm2,minm3)
var.min.48<-c("extremeness","expertise","case salience","case complexity","noncontiguous majority","vote uncertain","freshman","side (at 0 MQ distance)","distance to assignee\n(on moderate side)","closer to minority","side x distance to assignee","distance to assignee\n(on extreme side)","change in effect of distance,\nfrom moderate to extreme side","1 MQ away on moderate side vs.\n1 MQ away on extreme side")
se1.min.48<-se.fixef(mlm.min.48)
se.min.48<-c(se1.min.48[2:11],se1.min.48[16],minsd1,minsd2,minsd3)

conf.95<-c(qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.975),qnorm(.95),qnorm(.975),qnorm(.95),qnorm(.95),qnorm(.95),qnorm(.975),qnorm(.975))  ##vector fpr confidence int. Some tests are 2 tailed, some are one-tailed

conf.90<-c(qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.95),qnorm(.90),qnorm(.95),qnorm(.90),qnorm(.90),qnorm(.90),qnorm(.95),qnorm(.95))  ##vector fpr confidence int. Some tests are 2 tailed, some are one-tailed

graph.min <- -.7
graph.max <- .7
graph.by <- .1

y.axis <- c(length(coef.min.48):1)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis
pdf("CCOA_mlm_coeffs_fig3.pdf", height = 7, width = 6)  #open pdf device
par(mar=c(2, 9, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(coef.min.48, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        #set limits of x-axis so that they include mins and maxs of coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals
title(main = "Probit Coefficients, Minority Model",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(coef.min.48-conf.95*se.min.48, y.axis, coef.min.48+conf.95*se.min.48, y.axis, lwd =  1.2)#coef +/-1.96*se = 95% interval, lwd adjusts line thickness
segments(coef.min.48-conf.90*se.min.48, y.axis -.1, coef.min.48-conf.90*se.min.48, y.axis +.1, lwd = 1.1)#coef +/-1.64*se = 90% interval
segments(coef.min.48+conf.90*se.min.48, y.axis -.1, coef.min.48+conf.90*se.min.48, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T,          #draw x-axis and labels with tick marks
   cex.axis = .4, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
c(NA,-0.6,NA,-0.4,NA,-0.2,NA,0,NA,0.2,NA,0.4,NA,0.6,NA), tick = T, las  = 1,    #same as x-axis,but on top axis so
   line =0, cex.axis = .4, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = var.min.48, las = 1, tick = T,
   cex.axis = .5)                                       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)
abline(h=3.5)
#box (bty = "o") #place box around plot
#text(1.2, 5, expression(R^{2} == .15), adj = 0, cex = .7) #add text for R-squared
#text(1.2, 4, expression(paste("Adjusted ", R^{2} == .12, "")), adj = 0, cex= .7)#add text for Adjusted-R-squared
#text(1.2, 3, "n = 500", adj = 0, cex = .7)#add text for sample size
#segments(1.1, 2.7, 1.1,5.3)#use 3 segment commands to draw box around text; connect to  right-side of box
#segments(1.1, 2.7, 5,2.7)
#segments(1.1, 5.3, 5.2,5.3)
box() 
dev.off()


####### Predicted probabilities using mlm.maj.48.int

### subset the data for maj and min
maj <- subset(ccoa, confM.justice==1 & conf.total.M < 9)
min <- subset(ccoa, confM.justice==0 & conf.total.M < 9)

### note: nyt, laws are 1,2
maj$nyt<-as.numeric(maj$nyt)
maj$laws<-as.numeric(maj$laws)
min$nyt<-as.numeric(min$nyt)
min$laws<-as.numeric(min$laws)

### Laws (more than one legal provision)
laws.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
1,
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0) 

laws.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
2,
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)  

laws.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% laws.0))
laws.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% laws.1))
laws.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% laws.0))
laws.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% laws.1)) 

### Vote uncertain
uncert.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0) 

uncert.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,1,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0) 
  
uncert.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% uncert.0))
uncert.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% uncert.1))
uncert.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% uncert.0))
uncert.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% uncert.1))

### Noncontig 
noncontig.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
0,
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0) 

noncontig.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount+1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
1,
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0) 

noncontig.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% noncontig.0))
noncontig.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% noncontig.1))
noncontig.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% noncontig.0))
noncontig.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% noncontig.1))
 
#size5.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"5",1]))
#size5.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"5",1]))
#size5.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"5",1]))
#size5.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"5",1]))
   
#size6.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"6",1]))
#size6.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"6",1]))
#size6.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"6",1]))
#size6.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"6",1]))
   
#size7.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"7",1]))
#size7.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"7",1]))
#size7.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"7",1]))
#size7.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"7",1]))

#size8.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"8",1]))
#size8.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"8",1]))
#size8.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.0 + sim.mlm.maj.48.int$conf.total.M[,"8",1]))
#size8.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% size.1 + sim.mlm.maj.48.int$conf.total.M[,"8",1]))

### Freshman with mean experience vs. non-freshman with mean experience

fresh.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(subset(maj,freshman==1)$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,1,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

fresh.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% fresh.1))
fresh.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% fresh.1))

nonfresh.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(subset(maj,freshman==0)$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
mean(maj$IP.closer.min, na.rm=TRUE),
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

nonfresh.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonfresh.1))
nonfresh.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonfresh.1))

### Marginals, closermin and not

margclose.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,1,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

margclose.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,1,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

margclose.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% margclose.0))
margclose.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% margclose.1))
margclose.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% margclose.0))
margclose.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% margclose.1))

### non-Marginals, closermin and not

nonmargclose.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

nonmargclose.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
1,
mean(maj$IP.dist.assignee, na.rm=TRUE),
0,0,0,0)

nonmargclose.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonmargclose.0))
nonmargclose.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonmargclose.1))
nonmargclose.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonmargclose.0))
nonmargclose.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% nonmargclose.1))

### Assign to Marginal vs. twin, 1MQ away, 3MQ away
# marginal assignee type
margass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
1,1,0,0,0,
0,
mean(maj$IP.closer.min,na.rm=TRUE),
0,
1,0,0,0) 

# marginal "twin" assigned
margass.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,1,0,0,0,
0,
mean(maj$IP.closer.min,na.rm=TRUE),
0,
0,0,0,0)

# someone 1 MQ away assigned
mq1ass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,1,0,0,1,
1,
mean(maj$IP.closer.min,na.rm=TRUE),
1,
0,0,0,0)

# someone 10 MQ away assigned
mq10ass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,1,0,0,1,
10,
mean(maj$IP.closer.min,na.rm=TRUE),
10,
0,0,0,0)

margass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% margass.1))
margass.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% margass.0))
mq1ass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% mq1ass.1))
mq10ass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% mq10ass.1))
margass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% margass.1))
margass.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% margass.0))
mq1ass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% mq1ass.1))
mq10ass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% mq10ass.1))

#FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
### Assign to Closer.min vs. twin, 1MQ away, 3MQ away
# closer min assignee type
cmass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
1,0,0,0,0,
0,
1,
0,
0,0,0,1) 

# closer min "twin" assigned
cmass.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,0,
0,
1,
0,
0,0,0,0) 

# someone 1 MQ away assigned
cmmq1ass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
1,
1,
1,
0,0,0,0)

# someone 10 MQ away assigned
cmmq10ass.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
10,
1,
10,
0,0,0,0)

cmass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmass.1))
cmass.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmass.0))
cmmq1ass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmmq1ass.1))
cmmq10ass.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmmq10ass.1))
cmass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmass.1))
cmass.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmass.0))
cmmq1ass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmmq1ass.1))
cmmq10ass.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% cmmq10ass.1))


### Side and 1 and 10 MQ away
# side=1 and 1MQ away
sidemq1.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
1,
mean(maj$IP.closer.min,na.rm=TRUE),
1,
0,0,0,0)

# side=1 and 10 MQ away
sidemq10.1 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,1,
10,
mean(maj$IP.closer.min,na.rm=TRUE),
10,
0,0,0,0)

# side=0 and 1MQ away
sidemq1.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,0,
1,
mean(maj$IP.closer.min,na.rm=TRUE),
0,
0,0,0,0)

# side=1 and 10 MQ away
sidemq10.0 <- c(1,
mean(maj$IP.signed, na.rm=TRUE),
mean(log(maj$opcount + 1)),
mean(maj$nyt, na.rm=TRUE),
mean(maj$laws, na.rm=TRUE),
mean(maj$IP.noncontig, na.rm=TRUE),
0,0,0,0,0,
10,
mean(maj$IP.closer.min,na.rm=TRUE),
0,
0,0,0,0)

sidemq1.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq1.1))
sidemq10.prob1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq10.1))
sidemq1.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq1.0))
sidemq10.prob0 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq10.0))
sidemq1.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq1.1))
sidemq10.se1 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq10.1))
sidemq1.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq1.0))
sidemq10.se0 <- sd(pnorm(sim.mlm.maj.48.int$unmodeled %*% sidemq10.0))

# 1.intercept 2.log(opcount1) 3.nytsalient 4.laws 5.noncontig 6.assigned 7.marginal 8.uncertain 9.freshman 10.side 11.distance 12.closermin 13.side*distance 14.assigned*marginal 15.assigned*uncertain 16.assigned*freshman 17.assigned*closermin 

####### Predicted probabilities using mlm.min.48

### Laws (more than one legal provision)
laws.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
1,
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

laws.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
2,
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))  

laws.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% laws.m.0))
laws.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% laws.m.1))
laws.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% laws.m.0))
laws.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% laws.m.1)) 

### Salience nyt
nyt.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
1,
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

nyt.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
2,
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))  

nyt.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% nyt.m.0))
nyt.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% nyt.m.1))
nyt.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% nyt.m.0))
nyt.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% nyt.m.1))

### Vote uncertain
uncert.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(maj$IP.dist.assignee, na.rm=TRUE)) 

uncert.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
1,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(maj$IP.dist.assignee, na.rm=TRUE)) 
  
uncert.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% uncert.m.0))
uncert.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% uncert.m.1))
uncert.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% uncert.m.0))
uncert.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% uncert.m.1))

### Noncontig and size 5
noncontig5.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
0,
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
1,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

noncontig5.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
1,
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
1,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

noncontig5.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% noncontig5.m.0))
noncontig5.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% noncontig5.m.1))
noncontig5.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% noncontig5.m.0))
noncontig5.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% noncontig5.m.1))
 
### Noncontig and size 7
noncontig7.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
0,
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,1,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

noncontig7.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount+1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
1,
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,1,0,
mean(min$IP.dist.assignee, na.rm=TRUE)) 

noncontig7.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% noncontig7.m.0))
noncontig7.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% noncontig7.m.1))
noncontig7.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% noncontig7.m.0))
noncontig7.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% noncontig7.m.1)) 

### Freshman with mean experience vs. non-freshman with mean experience

fresh.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(subset(min,freshman==1)$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,1,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))

fresh.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% fresh.m.1))
fresh.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% fresh.m.1))

nonfresh.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(subset(min,freshman==0)$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
mean(min$IP.closer.min, na.rm=TRUE),
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))

nonfresh.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% nonfresh.m.1))
nonfresh.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% nonfresh.m.1))

close.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
0,
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))

close.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
mean(min$IP.dist.assignee, na.rm=TRUE),
1,
0,0,0,0,
mean(min$IP.dist.assignee, na.rm=TRUE))

close.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% close.m.0))
close.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% close.m.1))
close.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% close.m.0))
close.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% close.m.1))

### Side=1 and 0,1,and 10 MQ away

# side=0 and 0 MQ away
sidemq0.m.0 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,0,
0,
mean(min$IP.closer.min,na.rm=TRUE),
0,0,0,0,
0)

# side=1 and 0 MQ away
sidemq0.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
0,
mean(min$IP.closer.min,na.rm=TRUE),
0,0,0,0,
0)

# side=1 and 1 MQ away
sidemq1.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
1,
mean(min$IP.closer.min,na.rm=TRUE),
0,0,0,0,
1)

# side=1 and 10 MQ away
sidemq10.m.1 <- c(1,
mean(min$IP.signed, na.rm=TRUE),
mean(log(min$opcount + 1)),
mean(min$nyt, na.rm=TRUE),
mean(min$laws, na.rm=TRUE),
mean(min$IP.noncontig, na.rm=TRUE),
0,0,1,
10,
mean(min$IP.closer.min,na.rm=TRUE),
0,0,0,0,
10)

sidemq0.m.prob0 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% sidemq0.m.0))
sidemq0.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% sidemq0.m.1))
sidemq1.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% sidemq1.m.1))
sidemq10.m.prob1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% sidemq10.m.1))
sidemq0.m.se0 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% sidemq0.m.0))
sidemq0.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% sidemq0.m.1))
sidemq1.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% sidemq1.m.1))
sidemq10.m.se1 <- sd(pnorm(sim.mlm.min.48$unmodeled %*% sidemq10.m.1))


# 1.intercept 2.log(opcount1) 3.nytsalient 4.laws 5.noncontig 6.uncertain 7.freshman 8.side 9.distance 10.closermin 11.size5 12.size6 13.size7 14.size8 15.size*distance

####### Graphs of predicted probabilities using mlm.maj.48.int

prob <- c(laws.prob0,laws.prob1,NA,uncert.prob0,uncert.prob1,NA,noncontig.prob0,noncontig.prob1,NA,fresh.prob1,nonfresh.prob1,NA,margclose.prob0,margclose.prob1,nonmargclose.prob0,nonmargclose.prob1,NA,margass.prob1,margass.prob0,mq1ass.prob1,mq10ass.prob1,NA,cmass.prob1,cmass.prob0,cmmq1ass.prob1,cmmq10ass.prob1,NA,sidemq10.prob0,sidemq1.prob0,sidemq1.prob1,sidemq10.prob1)
prob.names<-c("one legal provision","more than one legal provision","vote not uncertain","vote uncertain","contiguous","non-contiguous","freshmen: mean experience","non-freshmen: mean experience","marginal: closer to majority","marginal: closer to minority","non-marginal: closer to majority","non-marginal: closer to minority","marginal: assigned","marginal: assigned to twin","marginal: assigned 1 MQ away", "marginal: assigned 10 MQ away","closer to min: assigned","closer to min: assigned to twin","closer to min: assigned 1 MQ away", "closer to min: assigned 10 MQ away","extreme side: 10 MQ away", "extreme side: 1 MQ away","moderate side: 1 MQ away", "moderate side: 10 MQ away")
prob.se <- c(laws.se0,laws.se1,NA,uncert.se0,uncert.se1,NA,noncontig.se0,noncontig.se1,NA,fresh.se1,nonfresh.se1,NA,margclose.se0,margclose.se1,nonmargclose.se0,nonmargclose.se1,NA,margass.se1,margass.se0,mq1ass.se1,mq10ass.se1,NA,cmass.se1,cmass.se0,cmmq1ass.se1,cmmq10ass.se1,NA,sidemq10.se0,sidemq1.se0,sidemq1.se1,sidemq10.se1)

graph.min <- 0
graph.max <- .35
graph.by <- .05

y.axis <- c(length(prob):1)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis
y.axis2 <- c(31,30,28,27,25,24,22,21,19,18,17,16,14,13,12,11,9,8,7,6,4,3,2,1)
pdf("CCOA_pred_probs.pdf", height = 7, width = 6)   #open pdf device
par(mar=c(2, 9, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(prob, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        #set limits of x-axis so that they include mins and maxs of coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals
title(main = "Majority Coalition Predicted Defection Probabilities",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(prob-qnorm(.95)*prob.se, y.axis, prob+qnorm(.95)*prob.se, y.axis, lwd =  1.1)#one-tailed test, lwd adjusts line thickness
segments(prob-qnorm(.90)*prob.se, y.axis -.1, prob-qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)#one-tailed test
segments(prob+qnorm(.90)*prob.se, y.axis -.1, prob+qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis2, label = prob.names, las = 1, cex.axis = .5)       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis2, lty = 3, col=2)
#box (bty = "o") #place box around plot
#text(1.2, 5, expression(R^{2} == .15), adj = 0, cex = .7) #add text for R-squared
#text(1.2, 4, expression(paste("Adjusted ", R^{2} == .12, "")), adj = 0, cex= .7)#add text for Adjusted-R-squared
#text(1.2, 3, "n = 500", adj = 0, cex = .7)#add text for sample size
#segments(1.1, 2.7, 1.1,5.3)#use 3 segment commands to draw box around text; connect to  right-side of box
#segments(1.1, 2.7, 5,2.7)
#segments(1.1, 5.3, 5.2,5.3)
box()
dev.off()


####### Graphs of predicted probabilities using mlm.min.48

prob <- c(laws.m.prob0,laws.m.prob1,NA,nyt.m.prob0,nyt.m.prob1,NA,uncert.m.prob0,uncert.m.prob1,NA,noncontig5.m.prob0,noncontig5.m.prob1,noncontig7.m.prob0,noncontig7.m.prob1,NA,fresh.m.prob1,nonfresh.m.prob1,NA,close.m.prob0,close.m.prob1,NA,sidemq0.m.prob0,sidemq0.m.prob1,sidemq1.m.prob1,sidemq10.m.prob1)

prob.names<-c("one legal provision","more than one legal provision","not salient","salient","vote not uncertain","vote uncertain","contiguous: majority size 5","non-contiguous: majority size 5","contiguous: majority size 7","non-contiguous: majority size 7","freshmen: mean experience","non-freshmen: mean experience","closer to majority","closer to minority","extreme side: 0 MQ away","moderate side: 0 MQ away","moderate side: 1 MQ away", "moderate side: 10 MQ away")
prob.se <- c(laws.m.se0,laws.m.se1,NA,nyt.m.se0,nyt.m.se1,NA,uncert.m.se0,uncert.m.se1,NA,noncontig5.m.se0,noncontig5.m.se1,noncontig7.m.se0,noncontig7.m.se1,NA,fresh.m.se1,nonfresh.m.se1,NA,close.m.se0,close.m.se1,NA,sidemq0.m.se0,sidemq0.m.se1,sidemq1.m.se1,sidemq10.m.se1)

graph.min <- 0
graph.max <- .35
graph.by <- .05

y.axis <- c(length(prob):1)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis
y.axis2 <- c(24,23,21,20,18,17,15,14,13,12,10,9,7,6,4,3,2,1)

pdf("CCOA_pred_probs_min.pdf", height = 7, width = 6)   #open pdf device
par(mar=c(2, 9, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(prob, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "")        #set limits of x-axis so that they include mins and maxs of coefficients + .95% confidence intervals and plot is symmetric; use "internal axes", and leave plot title empty the 3 lines below create horiztonal lines for 95% confidence intervals, and vertical ticks for 90% intervals
title(main = "Minority Coalition Predicted Conforming Probabilities",   cex.main = .8,
xlab = NULL, ylab = NULL,
     line = NA, outer = FALSE)
segments(prob-qnorm(.95)*prob.se, y.axis, prob+qnorm(.95)*prob.se, y.axis, lwd =  1.1)#one-tailed test, lwd adjusts line thickness
segments(prob-qnorm(.90)*prob.se, y.axis -.1, prob-qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)#one-tailed test
segments(prob+qnorm(.90)*prob.se, y.axis -.1, prob+qnorm(.90)*prob.se, y.axis +.1, lwd = 1.1)
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis2, label = prob.names, las = 1, tick = T, cex.axis = .5)         #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                                     # draw dotted line through 0
abline(h=y.axis2, lty = 3, col=2)
#box (bty = "o") #place box around plot
#text(1.2, 5, expression(R^{2} == .15), adj = 0, cex = .7) #add text for R-squared
#text(1.2, 4, expression(paste("Adjusted ", R^{2} == .12, "")), adj = 0, cex= .7)#add text for Adjusted-R-squared
#text(1.2, 3, "n = 500", adj = 0, cex = .7)#add text for sample size
#segments(1.1, 2.7, 1.1,5.3)#use 3 segment commands to draw box around text; connect to  right-side of box
#segments(1.1, 2.7, 5,2.7)
#segments(1.1, 5.3, 5.2,5.3)
box()
dev.off()

####### Graph of modeled effects from mlm.maj.48.int and mlm.min.48

### Justice

ranef <- rep(NA,ncol(sim.mlm.maj.48.int$justice))
ranef.se <- rep(NA,ncol(sim.mlm.maj.48.int$justice))
for (i in 1:ncol(sim.mlm.maj.48.int$justice)){
    ranef[i] <- mean(sim.mlm.maj.48.int$justice[,i,1])
    ranef.se[i] <- sd(sim.mlm.maj.48.int$justice[,i,1])
    }
ranef.names<-c("Black","Blackmun","Brennan","Burger","Burton","Clark","Douglas","Fortas","Frankfurter","Goldberg","Harlan","Jackson","Marshall","Minton","Murphy","O'Connor","Powell","Reed","Rehnquist","Rutledge","Stevens","Stewart","Vinson","Warren","White","Whittaker")
ranef.names.rank <- rep(NA,length(ranef.names))
for (j in 1:length(ranef.names)){
    ranef.names.rank[rank(ranef)[j]] <- ranef.names[j]
    }

graph.min <- -.4
graph.max <- .4
graph.by <- .2

y.axis <- c(length(ranef):1)

pdf("CCOA_ranef_justice.pdf", height = 7, width = 6)    #open pdf device
par(mar=c(2, 4, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
par(mfrow=c(1,2))
plot(ranef, rank(ranef), type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Justice Effects and Defection",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, rank(ranef), ranef+qnorm(.975)* ranef.se, rank(ranef), lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, rank(ranef) -.1, ranef-qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, rank(ranef) -.1, ranef+qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = seq(1,26,by=1), label = ranef.names.rank, las = 1, tick = T, cex.axis = .5)        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()



ranef <- rep(NA,ncol(sim.mlm.min.48$justice))
ranef.se <- rep(NA,ncol(sim.mlm.min.48$justice))
for (i in 1:ncol(sim.mlm.min.48$justice)){
    ranef[i] <- mean(sim.mlm.min.48$justice[,i,1])
    ranef.se[i] <- sd(sim.mlm.min.48$justice[,i,1])
    }
ranef.names<-c("Black","Blackmun","Brennan","Burger","Burton","Clark","Douglas","Fortas","Frankfurter","Goldberg","Harlan","Jackson","Marshall","Minton","Murphy","O'Connor","Powell","Reed","Rehnquist","Rutledge","Stevens","Stewart","Vinson","Warren","White","Whittaker")
ranef.names.rank <- rep(NA,length(ranef.names))
for (j in 1:length(ranef.names)){
    ranef.names.rank[rank(ranef)[j]] <- ranef.names[j]
    }


y.axis <- c(length(ranef):1)


plot(ranef, rank(ranef), type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Justice Effects and Conformity",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, rank(ranef), ranef+qnorm(.975)* ranef.se, rank(ranef), lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, rank(ranef) -.1, ranef-qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, rank(ranef) -.1, ranef+qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = seq(1,26,by=1), label = ranef.names.rank, las = 1, tick = T, cex.axis = .5)        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()
dev.off()




### conf.total.M

ranef <- rep(NA,ncol(sim.mlm.maj.48.int$conf.total.M))
ranef.se <- rep(NA,ncol(sim.mlm.maj.48.int$conf.total.M))
for (i in 1:ncol(sim.mlm.maj.48.int$conf.total.M)){
    ranef[i] <- mean(sim.mlm.maj.48.int$conf.total.M[,i,1])
    ranef.se[i] <- sd(sim.mlm.maj.48.int$conf.total.M[,i,1])
    }
ranef.names<-c(4,5,6,7,8)

graph.min <- -.4
graph.max <- .4
graph.by <- .1

y.axis <- c(length(ranef):1)

pdf("CCOA_ranef_conf_issue.pdf", height = 7, width = 6) #open pdf device
par(mar=c(2, 5, 4, 1.5))
layout(matrix(c(1,3,1,3,2,4,2,4,2,4),5,2, byrow = TRUE)) 
                        
plot(ranef, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Conference Majority Size Effects and Defection",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, y.axis, ranef+qnorm(.975)* ranef.se, y.axis, lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, y.axis -.03, ranef-qnorm(.95)*ranef.se, y.axis +.03, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, y.axis -.03, ranef+qnorm(.95)*ranef.se, y.axis +.03, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = ranef.names, las = 1, tick = T, cex.axis = .7)         #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()


### Issue

ranef <- rep(NA,ncol(sim.mlm.maj.48.int$value))
ranef.se <- rep(NA,ncol(sim.mlm.maj.48.int$value))
for (i in 1:ncol(sim.mlm.maj.48.int$value)){
    ranef[i] <- mean(sim.mlm.maj.48.int$value[,i,1])
    ranef.se[i] <- sd(sim.mlm.maj.48.int$value[,i,1])
    }
ranef.names<-c("criminal\nprocedure","civil\nrights","1st\namendment","due\nprocess","privacy","attorneys","unions","economic\nactivity","judicial\npower","federalism","federal\ntax")

ranef.names.rank <- rep(NA,length(ranef.names))
for (j in 1:length(ranef.names)){
    ranef.names.rank[rank(ranef)[j]] <- ranef.names[j]
    }

y.axis <- c(length(ranef):1)
                            
plot(ranef,rank(ranef), type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Issue Effects and Defection",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, rank(ranef), ranef+qnorm(.975)* ranef.se, rank(ranef), lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, rank(ranef) -.1, ranef-qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, rank(ranef) -.1, ranef+qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = seq(1,11,by=1), label = ranef.names.rank, las = 1, tick = T, cex.axis = .7)        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()


### conf.total.M

ranef <- rep(NA,4)
ranef.se <- rep(NA,4)
for (i in 12:15){
    ranef[i-11] <- mean(sim.mlm.maj.48.int$unmodeled[,i])
    ranef.se[i-11] <- sd(sim.mlm.maj.48.int$unmodeled[,i])
    }
ranef.names<-c(NA,5,6,7,8)

graph.min <- -.4
graph.max <- .4
graph.by <- .1

ranef<-c(NA,ranef)
ranef.se<-c(NA,ranef.se)

y.axis <- c(length(ranef):1)
    
plot(ranef, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Conference Majority Size Effects and Conformity",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, y.axis, ranef+qnorm(.975)* ranef.se, y.axis, lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, y.axis -.03, ranef-qnorm(.95)*ranef.se, y.axis +.03, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, y.axis -.03, ranef+qnorm(.95)*ranef.se, y.axis +.03, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = ranef.names, las = 1, tick = T, cex.axis = .7)         #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()

### Issue

ranef <- rep(NA,ncol(sim.mlm.min.48$value))
ranef.se <- rep(NA,ncol(sim.mlm.min.48$value))
for (i in 1:ncol(sim.mlm.min.48$value)){
    ranef[i] <- mean(sim.mlm.min.48$value[,i,1])
    ranef.se[i] <- sd(sim.mlm.min.48$value[,i,1])
    }
ranef.names<-c("criminal\nprocedure","civil\nrights","1st\namendment","due\nprocess","privacy","attorneys","unions","economic\nactivity","judicial\npower","federalism","federal\ntax")

ranef.names.rank <- rep(NA,length(ranef.names))
for (j in 1:length(ranef.names)){
    ranef.names.rank[rank(ranef)[j]] <- ranef.names[j]
    }

y.axis <- c(length(ranef):1)
                            
plot(ranef,rank(ranef), type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Issue Effects and Conformity",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, rank(ranef), ranef+qnorm(.975)* ranef.se, rank(ranef), lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, rank(ranef) -.1, ranef-qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, rank(ranef) -.1, ranef+qnorm(.95)*ranef.se, rank(ranef) +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .5, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = seq(1,11,by=1), label = ranef.names.rank, las = 1, tick = T, cex.axis = .7)        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()
dev.off()



### Natural Court

#rearranging in chron order

natct.coef<-cbind(sim.mlm.maj.48.int$natct[,8,1],sim.mlm.maj.48.int$natct[,9,1],sim.mlm.maj.48.int$natct[,10,1],sim.mlm.maj.48.int$natct[,13,1],sim.mlm.maj.48.int$natct[,14,1],sim.mlm.maj.48.int$natct[,15,1],sim.mlm.maj.48.int$natct[,16,1],sim.mlm.maj.48.int$natct[,17,1],sim.mlm.maj.48.int$natct[,18,1],sim.mlm.maj.48.int$natct[,19,1],sim.mlm.maj.48.int$natct[,20,1],sim.mlm.maj.48.int$natct[,11,1],sim.mlm.maj.48.int$natct[,12,1],sim.mlm.maj.48.int$natct[,1,1],sim.mlm.maj.48.int$natct[,2,1],sim.mlm.maj.48.int$natct[,3,1],sim.mlm.maj.48.int$natct[,4,1],sim.mlm.maj.48.int$natct[,5,1],sim.mlm.maj.48.int$natct[,6,1],sim.mlm.maj.48.int$natct[,7,1])

ranef <- rep(NA,ncol(natct.coef))
ranef.se <- rep(NA,ncol(natct.coef))
for (i in 1:ncol(natct.coef)){
    ranef[i] <- mean(natct.coef[,i])
    ranef.se[i] <- sd(natct.coef[,i])
    }

ranef.names<-c("VIN 1","VIN 2","WAR 1","WAR 2","WAR 3","WAR 4","WAR 5","WAR 6","WAR 7","WAR 8","WAR 9","WAR 10","WAR 11","BURG 1","BURG 2","BURG 3","BURG 4","BURG 5","BURG 6","BURG 7")

graph.min <- -.6
graph.max <- .6
graph.by <- .2

y.axis <- c(length(ranef):1)

pdf("CCOA_ranef_natct.pdf", height = 7, width = 6)  #open pdf device
par(mar=c(2, 3.5, 4, 1.5))                          #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
par(mfrow=c(1,2))
plot(ranef, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Natural Court Effects and Defection",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, y.axis, ranef+qnorm(.975)* ranef.se, y.axis, lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, y.axis -.1, ranef-qnorm(.95)*ranef.se, y.axis +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, y.axis -.1, ranef+qnorm(.95)*ranef.se, y.axis +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .4, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .4, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = ranef.names, las = 1, tick = T, cex.axis = .5)         #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()


### Natural Court

#rearranging in chron order

natct.coef<-cbind(sim.mlm.min.48$natct[,8,1],sim.mlm.min.48$natct[,9,1],sim.mlm.min.48$natct[,10,1],sim.mlm.min.48$natct[,13,1],sim.mlm.min.48$natct[,14,1],sim.mlm.min.48$natct[,15,1],sim.mlm.min.48$natct[,16,1],sim.mlm.min.48$natct[,17,1],sim.mlm.min.48$natct[,18,1],sim.mlm.min.48$natct[,19,1],sim.mlm.min.48$natct[,20,1],sim.mlm.min.48$natct[,11,1],sim.mlm.min.48$natct[,12,1],sim.mlm.min.48$natct[,1,1],sim.mlm.min.48$natct[,2,1],sim.mlm.min.48$natct[,3,1],sim.mlm.min.48$natct[,4,1],sim.mlm.min.48$natct[,5,1],sim.mlm.min.48$natct[,6,1],sim.mlm.min.48$natct[,7,1])

ranef <- rep(NA,ncol(natct.coef))
ranef.se <- rep(NA,ncol(natct.coef))
for (i in 1:ncol(natct.coef)){
    ranef[i] <- mean(natct.coef[,i])
    ranef.se[i] <- sd(natct.coef[,i])
    }

no.names <- (rep(NA,length(ranef)))

y.axis <- c(length(ranef):1)

plot(ranef, y.axis, type = "p", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      #plot coefficients as points, turning off axes and labels.
   xlim = c(graph.min,graph.max), xaxs = "i", main = "") 
title(main = "Natural Court Effects and Conformity",   cex.main = .8, xlab = NULL, ylab = NULL, line = NA, outer = FALSE)

segments(ranef-qnorm(.975)*ranef.se, y.axis, ranef+qnorm(.975)* ranef.se, y.axis, lwd =  1.1)
segments(ranef-qnorm(.95)*ranef.se, y.axis -.1, ranef-qnorm(.95)*ranef.se, y.axis +.1, lwd = 1.1)
segments(ranef+qnorm(.95)*ranef.se, y.axis -.1, ranef+qnorm(.95)*ranef.se, y.axis +.1, lwd = 1.1)

axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .4, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
axis(3, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T, las  = 1,  #same as x-axis,but on top axis so
   line =0, cex.axis = .4, mgp = c(2,.7,0))
#it's easier to lookup coefs at top of graph
axis(2, at = y.axis, label = no.names, las = 1, tick = T, cex.axis = .5)        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)        # draw dotted line through 0
abline(h=y.axis, lty = 3, col=2)

box()
dev.off()






####### Graph of prob switch over range of IP.dist.assignee using mlm.maj.48.int

graph.types <- rbind(rep(1,24),
rep(mean(maj$IP.signed, na.rm=TRUE),24),
rep(mean(log(maj$opcount + 1)),24),
rep(mean(maj$nyt, na.rm=TRUE),24),
rep(mean(maj$laws, na.rm=TRUE),24),
rep(mean(maj$IP.noncontig, na.rm=TRUE),24),
rep(0,24),rep(0,24),rep(0,24),rep(0,24),
c(rep(0,12),rep(1,12)),
c(11:0,0:11),
rep(mean(maj$IP.closer.min,na.rm=TRUE),24),
c(rep(0,12),0:11),
rep(0,24),rep(0,24),rep(0,24),rep(0,24))

graph.data <- pnorm((sim.mlm.maj.48.int$unmodeled)%*%graph.types)
graph.points <- rep(NA,ncol(graph.data))
graph.se <- rep(NA,ncol(graph.data))
for (i in 1:ncol(graph.data)){
    graph.points[i] <- mean(graph.data[,i])
    graph.se[i] <- sd(graph.data[,i])
    }

graph.min <- -11
graph.max <- 11
graph.by <- 2

x.axis <- c(-11:0,0:11)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis

pdf("CCOA_distance.pdf", height = 7, width = 6) #open pdf device
par(mar=c(4, 4, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
layout(matrix(c(1,1,1,2,2), 1, 5, byrow = TRUE))
plot(x.axis, graph.points, type = "n", axes = F, xlab = "Location of Assignee Relative to Justice", ylab = "Probability of Defection", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.2), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.20,by=.05), labels = seq(0,.2,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)    
text(x=c(-6,6),y=c(.008,.008),c('moderate side','extreme side'),cex=.6)                     
box()

####### Graph of prob switch 2

graph.min <- 0
graph.max <- 11
graph.by <- 2

x.axis <- c(11:0,0:11)#create indicator for y.axis, descending so that R orders vars from top to bottom on y-axis

plot(x.axis, graph.points, type = "n", axes = F, xlab = "Distance to Assignee", ylab = "", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.2), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3, col = 2) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3, col = 2) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.20,by=.05), labels = seq(0,.2,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))        #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                            
text(x=c(5),y=c(.115),c('extreme side'),adj=0,cex=.6)
text(x=c(6),y=c(.062),c('moderate side'),adj=1,cex=.6)                      
box()
dev.off()



####### Graph of prob switch over range of IP.dist.assignee using mlm.min.48

graph.types <- rbind(rep(1,24),
rep(mean(min$IP.signed, na.rm=TRUE),24),
rep(mean(log(min$opcount + 1)),24),
rep(mean(min$nyt, na.rm=TRUE),24),
rep(mean(min$laws, na.rm=TRUE),24),
rep(mean(min$IP.noncontig, na.rm=TRUE),24),
rep(0,24),
rep(0,24),
c(rep(0,12),rep(1,12)),
c(11:0,0:11),
rep(0,24),rep(0,24),rep(0,24),rep(0,24),
rep(mean(min$IP.closer.min,na.rm=TRUE),24),
c(rep(0,12),0:11))

graph.data <- pnorm((sim.mlm.min.48$unmodeled)%*%graph.types)
graph.points <- rep(NA,ncol(graph.data))
graph.se <- rep(NA,ncol(graph.data))
for (i in 1:ncol(graph.data)){
    graph.points[i] <- mean(graph.data[,i])
    graph.se[i] <- sd(graph.data[,i])
    }

graph.min <- -11
graph.max <- 11
graph.by <- 2

x.axis <- c(-11:0,0:11)

pdf("CCOA_distance_min.pdf", height = 7, width = 6) #open pdf device
par(mar=c(4, 4, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
layout(matrix(c(1,1,1,2,2), 1, 5, byrow = TRUE))
plot(x.axis, graph.points, type = "n", axes = F, xlab = "Location of Assignee Relative to Justice", ylab = "Probability of Conformity", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.7), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.70,by=.10), labels = seq(0,.70,by=.10), tick = T, cex.axis = .5, mgp = c(2,.5,0))       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)
text(x=c(-6,6),y=c(.02,.02),c('moderate side','extreme side'),cex=.6)                           
box()

graph.min <- 0
graph.max <- 11
graph.by <- 2

x.axis <- c(11:0,0:11)


plot(x.axis, graph.points, type = "n", axes = F, xlab = "Distance to Assignee", ylab = "", pch = 19,cex = .5,      
   xlim = c(graph.min,graph.max), xaxs = "i", ylim = c(0,.7), yaxs = "i", main = "") 
points(x.axis[1:12],graph.points[1:12], type = "b", pch = 19, cex = .5)
points(x.axis[13:24],graph.points[13:24], type = "b", pch = 19, cex = .5)
points(x.axis[1:12],graph.points[1:12]+qnorm(.95)*graph.se[1:12], type = "l", lty = 3)   
points(x.axis[1:12],graph.points[1:12]-qnorm(.95)*graph.se[1:12], type = "l", lty = 3) 
points(x.axis[13:24],graph.points[13:24]+qnorm(.95)*graph.se[13:24], type = "l", lty = 3)   
points(x.axis[13:24],graph.points[13:24]-qnorm(.95)*graph.se[13:24], type = "l", lty = 3) 
title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(graph.min,graph.max,by=graph.by ), labels =
seq(graph.min,graph.max,by=graph.by ), tick = T,            #draw x-axis and labels with tick marks
   cex.axis = .5, mgp = c(2,.5,0))                          #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.70,by=.10), labels = seq(0,.70,by=.10), tick = T, cex.axis = .5, mgp = c(2,.5,0))       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)    
text(x=c(6),y=c(.2),c('extreme side'),cex=.6, adj=1)
text(x=c(5),y=c(.46),c('moderate side'),cex=.6, adj=0)                      
                        
box()
dev.off()



######### Cohen v. California graph

cc <- subset(ccoa, us=="403/0015")

douglas.fact <- c(1,
subset(cc,justice=="Douglas")$IP.signed,
log(subset(cc,justice=="Douglas")$opcount + 1),
subset(cc,justice=="Douglas")$nyt,
subset(cc,justice=="Douglas")$laws,
subset(cc,justice=="Douglas")$IP.noncontig,
subset(cc,justice=="Douglas")$assignee.justice,
subset(cc,justice=="Douglas")$IP.marginal,
subset(cc,justice=="Douglas")$uncert.justice,
subset(cc,justice=="Douglas")$freshman,
subset(cc,justice=="Douglas")$IP.side,
subset(cc,justice=="Douglas")$IP.dist.assignee,
subset(cc,justice=="Douglas")$IP.closer.min,
subset(cc,justice=="Douglas")$IP.side*subset(cc,justice=="Douglas")$IP.dist.assignee,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$IP.marginal,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$uncert.justice,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$freshman,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$IP.closer.min
)
marshall.fact <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$IP.side,
subset(cc,justice=="Marshall")$IP.dist.assignee,
subset(cc,justice=="Marshall")$IP.closer.min,
subset(cc,justice=="Marshall")$IP.side*subset(cc,justice=="Marshall")$IP.dist.assignee,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)
stewart.fact <- c(1,
subset(cc,justice=="Stewart")$IP.signed,
log(subset(cc,justice=="Stewart")$opcount + 1),
subset(cc,justice=="Stewart")$nyt,
subset(cc,justice=="Stewart")$laws,
subset(cc,justice=="Stewart")$IP.noncontig,
subset(cc,justice=="Stewart")$assignee.justice,
subset(cc,justice=="Stewart")$IP.marginal,
subset(cc,justice=="Stewart")$uncert.justice,
subset(cc,justice=="Stewart")$freshman,
subset(cc,justice=="Stewart")$IP.side,
subset(cc,justice=="Stewart")$IP.dist.assignee,
subset(cc,justice=="Stewart")$IP.closer.min,
subset(cc,justice=="Stewart")$IP.side*subset(cc,justice=="Stewart")$IP.dist.assignee,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$IP.marginal,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$uncert.justice,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$freshman,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$IP.closer.min
)
harlan.fact <- c(1,
subset(cc,justice=="Harlan")$IP.signed,
log(subset(cc,justice=="Harlan")$opcount + 1),
subset(cc,justice=="Harlan")$nyt,
subset(cc,justice=="Harlan")$laws,
subset(cc,justice=="Harlan")$IP.noncontig,
subset(cc,justice=="Harlan")$assignee.justice,
subset(cc,justice=="Harlan")$IP.marginal,
1,
subset(cc,justice=="Harlan")$freshman,
subset(cc,justice=="Harlan")$IP.side,
subset(cc,justice=="Harlan")$IP.dist.assignee,
subset(cc,justice=="Harlan")$IP.closer.min,
subset(cc,justice=="Harlan")$IP.side*subset(cc,justice=="Harlan")$IP.dist.assignee,
subset(cc,justice=="Harlan")$assignee.justice*subset(cc,justice=="Harlan")$IP.marginal,
subset(cc,justice=="Harlan")$assignee.justice*1,
subset(cc,justice=="Harlan")$assignee.justice*subset(cc,justice=="Harlan")$freshman,
subset(cc,justice=="Harlan")$assignee.justice*subset(cc,justice=="Harlan")$IP.closer.min
)
brennan.fact <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
subset(cc,justice=="Brennan")$assignee.justice,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$IP.side,
subset(cc,justice=="Brennan")$IP.dist.assignee,
subset(cc,justice=="Brennan")$IP.closer.min,
subset(cc,justice=="Brennan")$IP.side*subset(cc,justice=="Brennan")$IP.dist.assignee,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.closer.min
)
white.fact <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
subset(cc,justice=="White")$IP.side,
subset(cc,justice=="White")$IP.dist.assignee,
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="White")$IP.side*subset(cc,justice=="White")$IP.dist.assignee)

blackmun.fact <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
subset(cc,justice=="Blackmun")$IP.side,
subset(cc,justice=="Blackmun")$IP.dist.assignee,
subset(cc,justice=="Blackmun")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Blackmun")$IP.side*subset(cc,justice=="Blackmun")$IP.dist.assignee)

burger.fact <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
subset(cc,justice=="Burger")$IP.side,
subset(cc,justice=="Burger")$IP.dist.assignee,
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Burger")$IP.side*subset(cc,justice=="Burger")$IP.dist.assignee)

black.fact <- c(1,
subset(cc,justice=="Black")$IP.signed,
log(subset(cc,justice=="Black")$opcount + 1),
subset(cc,justice=="Black")$nyt,
subset(cc,justice=="Black")$laws,
subset(cc,justice=="Black")$IP.noncontig,
subset(cc,justice=="Black")$uncert.justice,
subset(cc,justice=="Black")$freshman,
subset(cc,justice=="Black")$IP.side,
subset(cc,justice=="Black")$IP.dist.assignee,
subset(cc,justice=="Black")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Black")$IP.side*subset(cc,justice=="Black")$IP.dist.assignee)

case.fact.maj <- (sim.mlm.maj.48.int$conf.total.M[,"5",1]) + (sim.mlm.maj.48.int$value[,"1st amendment",1]) + (sim.mlm.maj.48.int$natct[,"BURG2",1])

case.fact.min <- (sim.mlm.min.48$value[,"1st amendment",1]) + (sim.mlm.min.48$natct[,"BURG2",1])

## majority justices
douglas.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% douglas.fact + sim.mlm.maj.48.int$justice[,"Douglas",1] + case.fact.maj))
marshall.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.fact + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
stewart.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stewart.fact + sim.mlm.maj.48.int$justice[,"Stewart",1] + case.fact.maj))
harlan.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% harlan.fact + sim.mlm.maj.48.int$justice[,"Harlan",1] + case.fact.maj))
brennan.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.fact + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))

## minority justices
white.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.fact + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
blackmun.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% blackmun.fact + sim.mlm.min.48$justice[,"Blackmun",1] + case.fact.min))
burger.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.fact + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
black.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% black.fact + sim.mlm.min.48$justice[,"Black",1] + case.fact.min))

### Douglas assigns to Stewart

douglas.cf1 <- c(1,
subset(cc,justice=="Douglas")$IP.signed,
log(subset(cc,justice=="Douglas")$opcount + 1),
subset(cc,justice=="Douglas")$nyt,
subset(cc,justice=="Douglas")$laws,
subset(cc,justice=="Douglas")$IP.noncontig,
subset(cc,justice=="Douglas")$assignee.justice,
subset(cc,justice=="Douglas")$IP.marginal,
subset(cc,justice=="Douglas")$uncert.justice,
subset(cc,justice=="Douglas")$freshman,
subset(cc,justice=="Douglas")$IP.side,
abs(subset(cc,justice=="Douglas")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Douglas")$IP.closer.min,
subset(cc,justice=="Douglas")$IP.side*abs(subset(cc,justice=="Douglas")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$IP.marginal,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$uncert.justice,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$freshman,
subset(cc,justice=="Douglas")$assignee.justice*subset(cc,justice=="Douglas")$IP.closer.min
)
marshall.cf1 <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$IP.side,
abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Marshall")$IP.closer.min,
subset(cc,justice=="Marshall")$IP.side*abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)
stewart.cf1 <- c(1,
subset(cc,justice=="Stewart")$IP.signed,
log(subset(cc,justice=="Stewart")$opcount + 1),
subset(cc,justice=="Stewart")$nyt,
subset(cc,justice=="Stewart")$laws,
subset(cc,justice=="Stewart")$IP.noncontig,
1,
subset(cc,justice=="Stewart")$IP.marginal,
subset(cc,justice=="Stewart")$uncert.justice,
subset(cc,justice=="Stewart")$freshman,
0,
0,
subset(cc,justice=="Stewart")$IP.closer.min,
0,
1*subset(cc,justice=="Stewart")$IP.marginal,
1*subset(cc,justice=="Stewart")$uncert.justice,
1*subset(cc,justice=="Stewart")$freshman,
1*subset(cc,justice=="Stewart")$IP.closer.min
)
harlan.cf1 <- c(1,
subset(cc,justice=="Harlan")$IP.signed,
log(subset(cc,justice=="Harlan")$opcount + 1),
subset(cc,justice=="Harlan")$nyt,
subset(cc,justice=="Harlan")$laws,
subset(cc,justice=="Harlan")$IP.noncontig,
0,
subset(cc,justice=="Harlan")$IP.marginal,
1,
subset(cc,justice=="Harlan")$freshman,
0,
abs(subset(cc,justice=="Harlan")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Harlan")$IP.closer.min,
0,
0,
0,
0,
0
)
brennan.cf1 <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
subset(cc,justice=="Brennan")$assignee.justice,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$IP.side,
abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Brennan")$IP.closer.min,
subset(cc,justice=="Brennan")$IP.side*(abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Stewart")$IP.justice)),
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.closer.min
)
white.cf1 <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
0,
abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
0)
blackmun.cf1 <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
subset(cc,justice=="Blackmun")$IP.side,
abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Blackmun")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Blackmun")$IP.side*(abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Stewart")$IP.justice)))
burger.cf1 <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
subset(cc,justice=="Burger")$IP.side,
abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Burger")$IP.side*(abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Stewart")$IP.justice)))
black.cf1 <- c(1,
subset(cc,justice=="Black")$IP.signed,
log(subset(cc,justice=="Black")$opcount + 1),
subset(cc,justice=="Black")$nyt,
subset(cc,justice=="Black")$laws,
subset(cc,justice=="Black")$IP.noncontig,
subset(cc,justice=="Black")$uncert.justice,
subset(cc,justice=="Black")$freshman,
subset(cc,justice=="Black")$IP.side,
abs(subset(cc,justice=="Black")$IP.justice - subset(cc,justice=="Stewart")$IP.justice),
subset(cc,justice=="Black")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Black")$IP.side*(abs(subset(cc,justice=="Black")$IP.justice - subset(cc,justice=="Stewart")$IP.justice)))

### majority justices
douglas.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% douglas.cf1 + sim.mlm.maj.48.int$justice[,"Douglas",1] + case.fact.maj))
marshall.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.cf1 + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
stewart.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stewart.cf1 + sim.mlm.maj.48.int$justice[,"Stewart",1] + case.fact.maj))
harlan.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% harlan.cf1 + sim.mlm.maj.48.int$justice[,"Harlan",1] + case.fact.maj))
brennan.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.cf1 + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))

## minority justices
white.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.cf1 + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
blackmun.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% blackmun.cf1 + sim.mlm.min.48$justice[,"Blackmun",1] + case.fact.min))
burger.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.cf1 + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
black.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% black.cf1 + sim.mlm.min.48$justice[,"Black",1] + case.fact.min))


### Douglas assigns to himself

douglas.cf2 <- c(1,
subset(cc,justice=="Douglas")$IP.signed,
log(subset(cc,justice=="Douglas")$opcount + 1),
subset(cc,justice=="Douglas")$nyt,
subset(cc,justice=="Douglas")$laws,
subset(cc,justice=="Douglas")$IP.noncontig,
1,
subset(cc,justice=="Douglas")$IP.marginal,
subset(cc,justice=="Douglas")$uncert.justice,
subset(cc,justice=="Douglas")$freshman,
0,
0,
subset(cc,justice=="Douglas")$IP.closer.min,
0,
1*subset(cc,justice=="Douglas")$IP.marginal,
1*subset(cc,justice=="Douglas")$uncert.justice,
1*subset(cc,justice=="Douglas")$freshman,
1*subset(cc,justice=="Douglas")$IP.closer.min
)
marshall.cf2 <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
1,
abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Marshall")$IP.closer.min,
1*abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)
stewart.cf2 <- c(1,
subset(cc,justice=="Stewart")$IP.signed,
log(subset(cc,justice=="Stewart")$opcount + 1),
subset(cc,justice=="Stewart")$nyt,
subset(cc,justice=="Stewart")$laws,
subset(cc,justice=="Stewart")$IP.noncontig,
subset(cc,justice=="Stewart")$assignee.justice,
subset(cc,justice=="Stewart")$IP.marginal,
subset(cc,justice=="Stewart")$uncert.justice,
subset(cc,justice=="Stewart")$freshman,
1,
abs(subset(cc,justice=="Stewart")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Stewart")$IP.closer.min,
1*(abs(subset(cc,justice=="Stewart")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)),
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$IP.marginal,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$uncert.justice,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$freshman,
subset(cc,justice=="Stewart")$assignee.justice*subset(cc,justice=="Stewart")$IP.closer.min
)
harlan.cf2 <- c(1,
subset(cc,justice=="Harlan")$IP.signed,
log(subset(cc,justice=="Harlan")$opcount + 1),
subset(cc,justice=="Harlan")$nyt,
subset(cc,justice=="Harlan")$laws,
subset(cc,justice=="Harlan")$IP.noncontig,
0,
subset(cc,justice=="Harlan")$IP.marginal,
1,
#subset(cc,justice=="Harlan")$uncert.justice,
subset(cc,justice=="Harlan")$freshman,
1,
abs(subset(cc,justice=="Harlan")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Harlan")$IP.closer.min,
1*(abs(subset(cc,justice=="Stewart")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)),
0,
0,
0,
0
)
brennan.cf2 <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
subset(cc,justice=="Brennan")$assignee.justice,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
1,
abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Brennan")$IP.closer.min,
1*(abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)),
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.closer.min
)
white.cf2 <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
1,
abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
1*(abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)))

blackmun.cf2 <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
1,
abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Blackmun")$IP.closer.min,
1,0,0,0,
1*(abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)))

burger.cf2 <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
1,
abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
1*(abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)))

black.cf2 <- c(1,
subset(cc,justice=="Black")$IP.signed,
log(subset(cc,justice=="Black")$opcount + 1),
subset(cc,justice=="Black")$nyt,
subset(cc,justice=="Black")$laws,
subset(cc,justice=="Black")$IP.noncontig,
subset(cc,justice=="Black")$uncert.justice,
subset(cc,justice=="Black")$freshman,
1,
abs(subset(cc,justice=="Black")$IP.justice - subset(cc,justice=="Douglas")$IP.justice),
subset(cc,justice=="Black")$IP.closer.min,
1,0,0,0,
1*(abs(subset(cc,justice=="Black")$IP.justice - subset(cc,justice=="Douglas")$IP.justice)))

### majority justices
douglas.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% douglas.cf2 + sim.mlm.maj.48.int$justice[,"Douglas",1] + case.fact.maj))
marshall.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.cf2 + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
stewart.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stewart.cf2 + sim.mlm.maj.48.int$justice[,"Stewart",1] + case.fact.maj))
harlan.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% harlan.cf2 + sim.mlm.maj.48.int$justice[,"Harlan",1] + case.fact.maj))
brennan.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.cf2 + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))

## minority justices
white.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.cf2 + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
blackmun.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% blackmun.cf2 + sim.mlm.min.48$justice[,"Blackmun",1] + case.fact.min))
burger.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.cf2 + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
black.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% black.cf2 + sim.mlm.min.48$justice[,"Black",1] + case.fact.min))


prob.fact <- c(douglas.prob.fact, brennan.prob.fact, marshall.prob.fact, harlan.prob.fact, stewart.prob.fact, black.prob.fact, white.prob.fact, blackmun.prob.fact, burger.prob.fact)

prob.cf1 <- c(douglas.prob.cf1, brennan.prob.cf1, marshall.prob.cf1, harlan.prob.cf1, stewart.prob.cf1, black.prob.cf1, white.prob.cf1, blackmun.prob.cf1, burger.prob.cf1)

prob.cf2 <- c(douglas.prob.cf2, brennan.prob.cf2, marshall.prob.cf2, harlan.prob.cf2, stewart.prob.cf2, black.prob.cf2, white.prob.cf2, blackmun.prob.cf2, burger.prob.cf2)


### graphiness

graph.min <- 0
graph.max <- .43
graph.by <- .05

x.axis <- c(1:9)

pdf("CCOA_casestudies.pdf", height = 7, width = 6)  #open pdf device
par(mar=c(4, 2, 4, 1.5))    
par(mfrow=c(1,2))
                        #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(x.axis, prob.fact, type = "n", axes = F, xlab = "", ylab = "Probability of Switch", pch = 19,cex = .5,      
   xlim = c(1,9), ylim = c(0,.43), yaxs = "i", main = "") 
points(x.axis[1:5],prob.fact[1:5], pch = 19,cex = .6, type = "o")   
points(x.axis[1:5],prob.cf1[1:5], pch = 19,cex = .6, type = "o",lty = 2)   
points(x.axis[1:5],prob.cf2[1:5], pch = 19,cex = .6, type = "o", lty = 3) 
points(x.axis[6:9],prob.fact[6:9], pch = 19,cex = .6, type = "o")   
points(x.axis[6:9],prob.cf1[6:9], pch = 19,cex = .6, type = "o",lty = 2)   
points(x.axis[6:9],prob.cf2[6:9], pch = 19,cex = .6, type = "o", lty = 3) 
title(main = "Cohen v. California",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(0,9,by= 1 ), labels = c(NA,"Douglas ","Brennan ","Marshall ","Harlan ","Stewart ","Black ","White ","Blackmun ","Burger "), tick = T,          #draw x-axis and labels with tick marks
   cex.axis = .6, mgp = c(2,.5,0), las=3)                           #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.43,by=.05), labels = seq(0,.43,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
text(x=c(5.2,5.2,5.2),y=c(.292,.263,.169),c('if Douglas is assigned','if Harlan is assigned','if Stewart is assigned'),adj=c(0),cex=.5)
box()

######### Bowers v. Hardwick graph

cc <- subset(ccoa, us=="478/0186")

marshall.fact <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$IP.side,
subset(cc,justice=="Marshall")$IP.dist.assignee,
subset(cc,justice=="Marshall")$IP.closer.min,
subset(cc,justice=="Marshall")$IP.side*subset(cc,justice=="Marshall")$IP.dist.assignee,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)

brennan.fact <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
subset(cc,justice=="Brennan")$assignee.justice,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$IP.side,
subset(cc,justice=="Brennan")$IP.dist.assignee,
subset(cc,justice=="Brennan")$IP.closer.min,
subset(cc,justice=="Brennan")$IP.side*subset(cc,justice=="Brennan")$IP.dist.assignee,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.closer.min
)

blackmun.fact <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
subset(cc,justice=="Blackmun")$assignee.justice,
subset(cc,justice=="Blackmun")$IP.marginal,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
subset(cc,justice=="Blackmun")$IP.side,
subset(cc,justice=="Blackmun")$IP.dist.assignee,
subset(cc,justice=="Blackmun")$IP.closer.min,
subset(cc,justice=="Blackmun")$IP.side*subset(cc,justice=="Blackmun")$IP.dist.assignee,
subset(cc,justice=="Blackmun")$assignee.justice*subset(cc,justice=="Blackmun")$IP.marginal,
subset(cc,justice=="Blackmun")$assignee.justice*subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$assignee.justice*subset(cc,justice=="Blackmun")$freshman,
subset(cc,justice=="Blackmun")$assignee.justice*subset(cc,justice=="Blackmun")$IP.closer.min
)

stevens.fact <- c(1,
subset(cc,justice=="Stevens")$IP.signed,
log(subset(cc,justice=="Stevens")$opcount + 1),
subset(cc,justice=="Stevens")$nyt,
subset(cc,justice=="Stevens")$laws,
subset(cc,justice=="Stevens")$IP.noncontig,
subset(cc,justice=="Stevens")$assignee.justice,
subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$freshman,
subset(cc,justice=="Stevens")$IP.side,
subset(cc,justice=="Stevens")$IP.dist.assignee,
subset(cc,justice=="Stevens")$IP.closer.min,
subset(cc,justice=="Stevens")$IP.side*subset(cc,justice=="Stevens")$IP.dist.assignee,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$freshman,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.closer.min
)

powell.fact <- c(1,
subset(cc,justice=="Powell")$IP.signed,
log(subset(cc,justice=="Powell")$opcount + 1),
subset(cc,justice=="Powell")$nyt,
subset(cc,justice=="Powell")$laws,
subset(cc,justice=="Powell")$IP.noncontig,
subset(cc,justice=="Powell")$assignee.justice,
subset(cc,justice=="Powell")$IP.marginal,
1,
subset(cc,justice=="Powell")$freshman,
subset(cc,justice=="Powell")$IP.side,
subset(cc,justice=="Powell")$IP.dist.assignee,
subset(cc,justice=="Powell")$IP.closer.min,
subset(cc,justice=="Powell")$IP.side*subset(cc,justice=="Powell")$IP.dist.assignee,
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$IP.marginal,
subset(cc,justice=="Powell")$assignee.justice*1,
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$freshman,
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$IP.closer.min
)

#minority
white.fact <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
subset(cc,justice=="White")$IP.side,
subset(cc,justice=="White")$IP.dist.assignee,
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="White")$IP.side*subset(cc,justice=="White")$IP.dist.assignee)

oconnor.fact <- c(1,
subset(cc,justice=="OConnor")$IP.signed,
log(subset(cc,justice=="OConnor")$opcount + 1),
subset(cc,justice=="OConnor")$nyt,
subset(cc,justice=="OConnor")$laws,
subset(cc,justice=="OConnor")$IP.noncontig,
subset(cc,justice=="OConnor")$uncert.justice,
subset(cc,justice=="OConnor")$freshman,
subset(cc,justice=="OConnor")$IP.side,
subset(cc,justice=="OConnor")$IP.dist.assignee,
subset(cc,justice=="OConnor")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="OConnor")$IP.side*subset(cc,justice=="OConnor")$IP.dist.assignee)

burger.fact <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
subset(cc,justice=="Burger")$IP.side,
subset(cc,justice=="Burger")$IP.dist.assignee,
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Burger")$IP.side*subset(cc,justice=="Burger")$IP.dist.assignee)

rehnquist.fact <- c(1,
subset(cc,justice=="Rehnquist")$IP.signed,
log(subset(cc,justice=="Rehnquist")$opcount + 1),
subset(cc,justice=="Rehnquist")$nyt,
subset(cc,justice=="Rehnquist")$laws,
subset(cc,justice=="Rehnquist")$IP.noncontig,
subset(cc,justice=="Rehnquist")$uncert.justice,
subset(cc,justice=="Rehnquist")$freshman,
subset(cc,justice=="Rehnquist")$IP.side,
subset(cc,justice=="Rehnquist")$IP.dist.assignee,
subset(cc,justice=="Rehnquist")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Rehnquist")$IP.side*subset(cc,justice=="Rehnquist")$IP.dist.assignee)

case.fact.maj <- (sim.mlm.maj.48.int$conf.total.M[,"5",1]) + (sim.mlm.maj.48.int$value[,"civil rights",1]) + (sim.mlm.maj.48.int$natct[,"BURG7",1])

case.fact.min <- (sim.mlm.min.48$value[,"civil rights",1]) + (sim.mlm.min.48$natct[,"BURG7",1])

## majority justices
marshall.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.fact + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
brennan.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.fact + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))
blackmun.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% blackmun.fact + sim.mlm.maj.48.int$justice[,"Blackmun",1] + case.fact.maj))
stevens.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stevens.fact + sim.mlm.maj.48.int$justice[,"Stevens",1] + case.fact.maj))
powell.prob.fact <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% powell.fact + sim.mlm.maj.48.int$justice[,"Powell",1] + case.fact.maj))

## minority justices
white.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.fact + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
oconnor.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% oconnor.fact + sim.mlm.min.48$justice[,"OConnor",1] + case.fact.min))
burger.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.fact + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
rehnquist.prob.fact <- mean(pnorm(sim.mlm.min.48$unmodeled %*% rehnquist.fact + sim.mlm.min.48$justice[,"Rehnquist",1] + case.fact.min))

### Brennan assigns to Powell

marshall.cf1 <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$IP.side,
abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Marshall")$IP.closer.min,
subset(cc,justice=="Marshall")$IP.side*abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)

brennan.cf1 <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
subset(cc,justice=="Brennan")$assignee.justice,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$IP.side,
abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Brennan")$IP.closer.min,
subset(cc,justice=="Brennan")$IP.side*abs(subset(cc,justice=="Brennan")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$freshman,
subset(cc,justice=="Brennan")$assignee.justice*subset(cc,justice=="Brennan")$IP.closer.min
)

blackmun.cf1 <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
0,
subset(cc,justice=="Blackmun")$IP.marginal,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
0,
abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Blackmun")$IP.closer.min,
0,
0,
0,
0,
0
)

stevens.cf1 <- c(1,
subset(cc,justice=="Stevens")$IP.signed,
log(subset(cc,justice=="Stevens")$opcount + 1),
subset(cc,justice=="Stevens")$nyt,
subset(cc,justice=="Stevens")$laws,
subset(cc,justice=="Stevens")$IP.noncontig,
subset(cc,justice=="Stevens")$assignee.justice,
subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$freshman,
0,
abs(subset(cc,justice=="Stevens")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Stevens")$IP.closer.min,
0,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$freshman,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.closer.min
)

powell.cf1 <- c(1,
subset(cc,justice=="Powell")$IP.signed,
log(subset(cc,justice=="Powell")$opcount + 1),
subset(cc,justice=="Powell")$nyt,
subset(cc,justice=="Powell")$laws,
subset(cc,justice=="Powell")$IP.noncontig,
1,
subset(cc,justice=="Powell")$IP.marginal,
1,
subset(cc,justice=="Powell")$freshman,
0,
0,
subset(cc,justice=="Powell")$IP.closer.min,
0,
1*subset(cc,justice=="Powell")$IP.marginal,
1*1,
1*subset(cc,justice=="Powell")$freshman,
1*subset(cc,justice=="Powell")$IP.closer.min
)

##minority

white.cf1 <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
subset(cc,justice=="White")$IP.side,
abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="White")$IP.side*(abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Powell")$IP.justice)))

oconnor.cf1 <- c(1,
subset(cc,justice=="OConnor")$IP.signed,
log(subset(cc,justice=="OConnor")$opcount + 1),
subset(cc,justice=="OConnor")$nyt,
subset(cc,justice=="OConnor")$laws,
subset(cc,justice=="OConnor")$IP.noncontig,
subset(cc,justice=="OConnor")$uncert.justice,
subset(cc,justice=="OConnor")$freshman,
subset(cc,justice=="OConnor")$IP.side,
abs(subset(cc,justice=="OConnor")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="OConnor")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="OConnor")$IP.side*(abs(subset(cc,justice=="OConnor")$IP.justice - subset(cc,justice=="Powell")$IP.justice)))

burger.cf1 <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
subset(cc,justice=="Burger")$IP.side,
abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Burger")$IP.side*(abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Powell")$IP.justice)))

rehnquist.cf1 <- c(1,
subset(cc,justice=="Rehnquist")$IP.signed,
log(subset(cc,justice=="Rehnquist")$opcount + 1),
subset(cc,justice=="Rehnquist")$nyt,
subset(cc,justice=="Rehnquist")$laws,
subset(cc,justice=="Rehnquist")$IP.noncontig,
subset(cc,justice=="Rehnquist")$uncert.justice,
subset(cc,justice=="Rehnquist")$freshman,
subset(cc,justice=="Rehnquist")$IP.side,
abs(subset(cc,justice=="Rehnquist")$IP.justice - subset(cc,justice=="Powell")$IP.justice),
subset(cc,justice=="Rehnquist")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Rehnquist")$IP.side*(abs(subset(cc,justice=="Rehnquist")$IP.justice - subset(cc,justice=="Powell")$IP.justice)))

### majority justices
marshall.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.cf1 + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
brennan.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.cf1 + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))
blackmun.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% blackmun.cf1 + sim.mlm.maj.48.int$justice[,"Blackmun",1] + case.fact.maj))
stevens.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stevens.cf1 + sim.mlm.maj.48.int$justice[,"Stevens",1] + case.fact.maj))
powell.prob.cf1 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% powell.cf1 + sim.mlm.maj.48.int$justice[,"Powell",1] + case.fact.maj))

## minority justices
white.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.cf1 + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
oconnor.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% oconnor.cf1 + sim.mlm.min.48$justice[,"OConnor",1] + case.fact.min))
burger.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.cf1 + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
rehnquist.prob.cf1 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% rehnquist.cf1 + sim.mlm.min.48$justice[,"Rehnquist",1] + case.fact.min))


### Brennan assigns to himself

marshall.cf2 <- c(1,
subset(cc,justice=="Marshall")$IP.signed,
log(subset(cc,justice=="Marshall")$opcount + 1),
subset(cc,justice=="Marshall")$nyt,
subset(cc,justice=="Marshall")$laws,
subset(cc,justice=="Marshall")$IP.noncontig,
subset(cc,justice=="Marshall")$assignee.justice,
subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$IP.side,
abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Marshall")$IP.closer.min,
subset(cc,justice=="Marshall")$IP.side*abs(subset(cc,justice=="Marshall")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.marginal,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$uncert.justice,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$freshman,
subset(cc,justice=="Marshall")$assignee.justice*subset(cc,justice=="Marshall")$IP.closer.min
)

brennan.cf2 <- c(1,
subset(cc,justice=="Brennan")$IP.signed,
log(subset(cc,justice=="Brennan")$opcount + 1),
subset(cc,justice=="Brennan")$nyt,
subset(cc,justice=="Brennan")$laws,
subset(cc,justice=="Brennan")$IP.noncontig,
1,
subset(cc,justice=="Brennan")$IP.marginal,
subset(cc,justice=="Brennan")$uncert.justice,
subset(cc,justice=="Brennan")$freshman,
0,
0,
subset(cc,justice=="Brennan")$IP.closer.min,
0,
1*subset(cc,justice=="Brennan")$IP.marginal,
1*subset(cc,justice=="Brennan")$uncert.justice,
1*subset(cc,justice=="Brennan")$freshman,
1*subset(cc,justice=="Brennan")$IP.closer.min
)

blackmun.cf2 <- c(1,
subset(cc,justice=="Blackmun")$IP.signed,
log(subset(cc,justice=="Blackmun")$opcount + 1),
subset(cc,justice=="Blackmun")$nyt,
subset(cc,justice=="Blackmun")$laws,
subset(cc,justice=="Blackmun")$IP.noncontig,
0,
subset(cc,justice=="Blackmun")$IP.marginal,
subset(cc,justice=="Blackmun")$uncert.justice,
subset(cc,justice=="Blackmun")$freshman,
1,
abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Blackmun")$IP.closer.min,
1*abs(subset(cc,justice=="Blackmun")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
0,
0,
0,
0
)

stevens.cf2 <- c(1,
subset(cc,justice=="Stevens")$IP.signed,
log(subset(cc,justice=="Stevens")$opcount + 1),
subset(cc,justice=="Stevens")$nyt,
subset(cc,justice=="Stevens")$laws,
subset(cc,justice=="Stevens")$IP.noncontig,
subset(cc,justice=="Stevens")$assignee.justice,
subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$freshman,
1,
abs(subset(cc,justice=="Stevens")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Stevens")$IP.closer.min,
1*abs(subset(cc,justice=="Stevens")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.marginal,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$uncert.justice,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$freshman,
subset(cc,justice=="Stevens")$assignee.justice*subset(cc,justice=="Stevens")$IP.closer.min
)

powell.cf2 <- c(1,
subset(cc,justice=="Powell")$IP.signed,
log(subset(cc,justice=="Powell")$opcount + 1),
subset(cc,justice=="Powell")$nyt,
subset(cc,justice=="Powell")$laws,
subset(cc,justice=="Powell")$IP.noncontig,
0,
subset(cc,justice=="Powell")$IP.marginal,
1,
subset(cc,justice=="Powell")$freshman,
1,
abs(subset(cc,justice=="Powell")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Powell")$IP.closer.min,
1*abs(subset(cc,justice=="Powell")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$IP.marginal,
subset(cc,justice=="Powell")$assignee.justice*1,
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$freshman,
subset(cc,justice=="Powell")$assignee.justice*subset(cc,justice=="Powell")$IP.closer.min
)

##minority

white.cf2 <- c(1,
subset(cc,justice=="White")$IP.signed,
log(subset(cc,justice=="White")$opcount + 1),
subset(cc,justice=="White")$nyt,
subset(cc,justice=="White")$laws,
subset(cc,justice=="White")$IP.noncontig,
subset(cc,justice=="White")$uncert.justice,
subset(cc,justice=="White")$freshman,
subset(cc,justice=="White")$IP.side,
abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="White")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="White")$IP.side*(abs(subset(cc,justice=="White")$IP.justice - subset(cc,justice=="Brennan")$IP.justice)))

oconnor.cf2 <- c(1,
subset(cc,justice=="OConnor")$IP.signed,
log(subset(cc,justice=="OConnor")$opcount + 1),
subset(cc,justice=="OConnor")$nyt,
subset(cc,justice=="OConnor")$laws,
subset(cc,justice=="OConnor")$IP.noncontig,
subset(cc,justice=="OConnor")$uncert.justice,
subset(cc,justice=="OConnor")$freshman,
subset(cc,justice=="OConnor")$IP.side,
abs(subset(cc,justice=="OConnor")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="OConnor")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="OConnor")$IP.side*(abs(subset(cc,justice=="OConnor")$IP.justice - subset(cc,justice=="Brennan")$IP.justice)))

burger.cf2 <- c(1,
subset(cc,justice=="Burger")$IP.signed,
log(subset(cc,justice=="Burger")$opcount + 1),
subset(cc,justice=="Burger")$nyt,
subset(cc,justice=="Burger")$laws,
subset(cc,justice=="Burger")$IP.noncontig,
subset(cc,justice=="Burger")$uncert.justice,
subset(cc,justice=="Burger")$freshman,
subset(cc,justice=="Burger")$IP.side,
abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Burger")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Burger")$IP.side*(abs(subset(cc,justice=="Burger")$IP.justice - subset(cc,justice=="Brennan")$IP.justice)))

rehnquist.cf2 <- c(1,
subset(cc,justice=="Rehnquist")$IP.signed,
log(subset(cc,justice=="Rehnquist")$opcount + 1),
subset(cc,justice=="Rehnquist")$nyt,
subset(cc,justice=="Rehnquist")$laws,
subset(cc,justice=="Rehnquist")$IP.noncontig,
subset(cc,justice=="Rehnquist")$uncert.justice,
subset(cc,justice=="Rehnquist")$freshman,
subset(cc,justice=="Rehnquist")$IP.side,
abs(subset(cc,justice=="Rehnquist")$IP.justice - subset(cc,justice=="Brennan")$IP.justice),
subset(cc,justice=="Rehnquist")$IP.closer.min,
1,0,0,0,
subset(cc,justice=="Rehnquist")$IP.side*(abs(subset(cc,justice=="Rehnquist")$IP.justice - subset(cc,justice=="Brennan")$IP.justice)))

### majority justices
marshall.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% marshall.cf2 + sim.mlm.maj.48.int$justice[,"Marshall",1] + case.fact.maj))
brennan.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% brennan.cf2 + sim.mlm.maj.48.int$justice[,"Brennan",1] + case.fact.maj))
blackmun.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% blackmun.cf2 + sim.mlm.maj.48.int$justice[,"Blackmun",1] + case.fact.maj))
stevens.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% stevens.cf2 + sim.mlm.maj.48.int$justice[,"Stevens",1] + case.fact.maj))
powell.prob.cf2 <- mean(pnorm(sim.mlm.maj.48.int$unmodeled %*% powell.cf2 + sim.mlm.maj.48.int$justice[,"Powell",1] + case.fact.maj))

## minority justices
white.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% white.cf2 + sim.mlm.min.48$justice[,"White",1] + case.fact.min))
oconnor.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% oconnor.cf2 + sim.mlm.min.48$justice[,"OConnor",1] + case.fact.min))
burger.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% burger.cf2 + sim.mlm.min.48$justice[,"Burger",1] + case.fact.min))
rehnquist.prob.cf2 <- mean(pnorm(sim.mlm.min.48$unmodeled %*% rehnquist.cf2 + sim.mlm.min.48$justice[,"Rehnquist",1] + case.fact.min))


prob.fact <- c(marshall.prob.fact, brennan.prob.fact, blackmun.prob.fact, stevens.prob.fact, powell.prob.fact, white.prob.fact, oconnor.prob.fact, burger.prob.fact, rehnquist.prob.fact)

prob.cf1 <- c(marshall.prob.cf1, brennan.prob.cf1, blackmun.prob.cf1, stevens.prob.cf1, powell.prob.cf1, white.prob.cf1, oconnor.prob.cf1, burger.prob.cf1, rehnquist.prob.cf1)

prob.cf2 <- c(marshall.prob.cf2, brennan.prob.cf2, blackmun.prob.cf2, stevens.prob.cf2, powell.prob.cf2, white.prob.cf2, oconnor.prob.cf2, burger.prob.cf2, rehnquist.prob.cf2)


### graphiness

par(mar=c(4, 2, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names

plot(x.axis, prob.fact, type = "n", axes = F, xlab = "", ylab = "", pch = 19,cex = .5,      
   xlim = c(1,9), ylim = c(0,.43), yaxs = "i", main = "") 
points(x.axis[1:5],prob.fact[1:5], pch = 19,cex = .6, type = "o")   
points(x.axis[1:5],prob.cf1[1:5], pch = 19,cex = .6, type = "o",lty = 2)   
points(x.axis[1:5],prob.cf2[1:5], pch = 19,cex = .6, type = "o", lty = 3) 
points(x.axis[6:9],prob.fact[6:9], pch = 19,cex = .6, type = "o")   
points(x.axis[6:9],prob.cf1[6:9], pch = 19,cex = .6, type = "o",lty = 2)   
points(x.axis[6:9],prob.cf2[6:9], pch = 19,cex = .6, type = "o", lty = 3) 
title(main = "Bowers v. Hardwick",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(0,9,by= 1 ), labels = c(NA,"Marshall ","Brennan ","Blackmun ","Stevens ","Powell ","White ","O'Connor ","Burger ","Rehnquist "), tick = T,             #draw x-axis and labels with tick marks
   cex.axis = .6, mgp = c(2,.5,0), las=3)                           #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,.43,by=.05), labels = seq(0,.43,by=.05), tick = T, cex.axis = .5, mgp = c(2,.5,0))       #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
text(x=c(5.2,5.2,5.2),y=c(.4,.387,.228),c('if Brennan is assigned','if Blackmun is assigned','if Powell is assigned'),adj=c(0),cex=.5)
box()
dev.off()





##### Predicted probs from the data (mlm.maj.48.int)

maj$issue <- pmin(as.numeric(maj$value),11)

maj.pred.data <- cbind(rep(1,nrow(maj)), maj$IP.signed, log(maj$opcount+1), maj$nyt, maj$laws, maj$IP.noncontig, maj$assignee.justice, maj$IP.marginal, maj$uncert.justice, maj$freshman, maj$IP.side, maj$IP.dist.assignee, maj$IP.closer.min, maj$IP.side*maj$IP.dist.assignee, maj$assignee.justice*maj$IP.marginal, maj$assignee.justice*maj$uncert.justice, maj$assignee.justice*maj$freshman, maj$assignee.justice*maj$IP.closer.min) 

maj.pred.prob <- pnorm(maj.pred.data %*% cbind(fixef(mlm.maj.48.int)) +  ranef(mlm.maj.48.int)$justice[maj$justice,1] + ranef(mlm.maj.48.int)$natct[maj$natct,1] + ranef(mlm.maj.48.int)$conf.total.M[maj$conf.total.M-3,1] + ranef(mlm.maj.48.int)$value[maj$issue,1])

##### Predicted probs from the data (mlm.maj.48.int)

min$issue <- pmin(as.numeric(min$value),11)

min.data <- (cbind(rep(1,nrow(min)), min$IP.signed, log(min$opcount+1), min$nyt, min$laws, min$IP.noncontig, min$uncert.justice, min$freshman, min$IP.side, min$IP.dist.assignee, min$IP.closer.min, min$conf.total.M, min$IP.side*min$IP.dist.assignee) )

min.prob.data4 <- (cbind(rep(1,nrow(subset(min,conf.total.M==4))), (subset(min,conf.total.M==4))$IP.signed, log((subset(min,conf.total.M==4))$opcount+1), (subset(min,conf.total.M==4))$nyt, (subset(min,conf.total.M==4))$laws, (subset(min,conf.total.M==4))$IP.noncontig, (subset(min,conf.total.M==4))$uncert.justice, (subset(min,conf.total.M==4))$freshman, (subset(min,conf.total.M==4))$IP.side, (subset(min,conf.total.M==4))$IP.dist.assignee, (subset(min,conf.total.M==4))$IP.closer.min, rep(0,nrow(subset(min,conf.total.M==4))),rep(0,nrow(subset(min,conf.total.M==4))),rep(0,nrow(subset(min,conf.total.M==4))),rep(0,nrow(subset(min,conf.total.M==4))), (subset(min,conf.total.M==4))$IP.side*(subset(min,conf.total.M==4))$IP.dist.assignee) )

min.prob.data5 <- (cbind(rep(1,nrow(subset(min,conf.total.M==5))), (subset(min,conf.total.M==5))$IP.signed, log((subset(min,conf.total.M==5))$opcount+1), (subset(min,conf.total.M==5))$nyt, (subset(min,conf.total.M==5))$laws, (subset(min,conf.total.M==5))$IP.noncontig, (subset(min,conf.total.M==5))$uncert.justice, (subset(min,conf.total.M==5))$freshman, (subset(min,conf.total.M==5))$IP.side, (subset(min,conf.total.M==5))$IP.dist.assignee, (subset(min,conf.total.M==5))$IP.closer.min, rep(1,nrow(subset(min,conf.total.M==5))),rep(0,nrow(subset(min,conf.total.M==5))),rep(0,nrow(subset(min,conf.total.M==5))),rep(0,nrow(subset(min,conf.total.M==5))), (subset(min,conf.total.M==5))$IP.side*(subset(min,conf.total.M==5))$IP.dist.assignee) )

min.prob.data6 <- (cbind(rep(1,nrow(subset(min,conf.total.M==6))), (subset(min,conf.total.M==6))$IP.signed, log((subset(min,conf.total.M==6))$opcount+1), (subset(min,conf.total.M==6))$nyt, (subset(min,conf.total.M==6))$laws, (subset(min,conf.total.M==6))$IP.noncontig, (subset(min,conf.total.M==6))$uncert.justice, (subset(min,conf.total.M==6))$freshman, (subset(min,conf.total.M==6))$IP.side, (subset(min,conf.total.M==6))$IP.dist.assignee, (subset(min,conf.total.M==6))$IP.closer.min, rep(0,nrow(subset(min,conf.total.M==6))),rep(1,nrow(subset(min,conf.total.M==6))),rep(0,nrow(subset(min,conf.total.M==6))),rep(0,nrow(subset(min,conf.total.M==6))), (subset(min,conf.total.M==6))$IP.side*(subset(min,conf.total.M==6))$IP.dist.assignee) )

min.prob.data7 <- (cbind(rep(1,nrow(subset(min,conf.total.M==7))), (subset(min,conf.total.M==7))$IP.signed, log((subset(min,conf.total.M==7))$opcount+1), (subset(min,conf.total.M==7))$nyt, (subset(min,conf.total.M==7))$laws, (subset(min,conf.total.M==7))$IP.noncontig, (subset(min,conf.total.M==7))$uncert.justice, (subset(min,conf.total.M==7))$freshman, (subset(min,conf.total.M==7))$IP.side, (subset(min,conf.total.M==7))$IP.dist.assignee, (subset(min,conf.total.M==7))$IP.closer.min, rep(0,nrow(subset(min,conf.total.M==7))),rep(0,nrow(subset(min,conf.total.M==7))),rep(1,nrow(subset(min,conf.total.M==7))),rep(0,nrow(subset(min,conf.total.M==7))), (subset(min,conf.total.M==7))$IP.side*(subset(min,conf.total.M==7))$IP.dist.assignee) )

min.prob.data8 <- (cbind(rep(1,nrow(subset(min,conf.total.M==8))), (subset(min,conf.total.M==8))$IP.signed, log((subset(min,conf.total.M==8))$opcount+1), (subset(min,conf.total.M==8))$nyt, (subset(min,conf.total.M==8))$laws, (subset(min,conf.total.M==8))$IP.noncontig, (subset(min,conf.total.M==8))$uncert.justice, (subset(min,conf.total.M==8))$freshman, (subset(min,conf.total.M==8))$IP.side, (subset(min,conf.total.M==8))$IP.dist.assignee, (subset(min,conf.total.M==8))$IP.closer.min, rep(0,nrow(subset(min,conf.total.M==8))),rep(0,nrow(subset(min,conf.total.M==8))),rep(0,nrow(subset(min,conf.total.M==8))),rep(1,nrow(subset(min,conf.total.M==8))), (subset(min,conf.total.M==8))$IP.side*(subset(min,conf.total.M==8))$IP.dist.assignee) )

min.pred.prob4 <- pnorm(min.prob.data4 %*% cbind(fixef(mlm.min.48)) +  ranef(mlm.min.48)$justice[(subset(min,conf.total.M==4))$justice,1] + ranef(mlm.min.48)$natct[(subset(min,conf.total.M==4))$natct,1] + ranef(mlm.min.48)$value[(subset(min,conf.total.M==4))$issue,1])

min.pred.prob5 <- pnorm(min.prob.data5 %*% cbind(fixef(mlm.min.48)) +  ranef(mlm.min.48)$justice[(subset(min,conf.total.M==5))$justice,1] + ranef(mlm.min.48)$natct[(subset(min,conf.total.M==5))$natct,1] + ranef(mlm.min.48)$value[(subset(min,conf.total.M==5))$issue,1])

min.pred.prob6 <- pnorm(min.prob.data6 %*% cbind(fixef(mlm.min.48)) +  ranef(mlm.min.48)$justice[(subset(min,conf.total.M==6))$justice,1] + ranef(mlm.min.48)$natct[(subset(min,conf.total.M==6))$natct,1] + ranef(mlm.min.48)$value[(subset(min,conf.total.M==6))$issue,1])

min.pred.prob7 <- pnorm(min.prob.data7 %*% cbind(fixef(mlm.min.48)) +  ranef(mlm.min.48)$justice[(subset(min,conf.total.M==7))$justice,1] + ranef(mlm.min.48)$natct[(subset(min,conf.total.M==7))$natct,1] + ranef(mlm.min.48)$value[(subset(min,conf.total.M==7))$issue,1])

min.pred.prob8 <- pnorm(min.prob.data8 %*% cbind(fixef(mlm.min.48)) +  ranef(mlm.min.48)$justice[(subset(min,conf.total.M==8))$justice,1] + ranef(mlm.min.48)$natct[(subset(min,conf.total.M==8))$natct,1] + ranef(mlm.min.48)$value[(subset(min,conf.total.M==8))$issue,1])

min.pred.prob <- c(min.pred.prob4,min.pred.prob5,min.pred.prob6,min.pred.prob7,min.pred.prob8)

### mean squared error

### mlm.maj.48.int

mse.maj <- mean((maj$switch.rev-maj.pred.prob)^2, na.rm=TRUE)
mse.min <- mean((min$switch.rev-min.pred.prob)^2, na.rm=TRUE)







graph.min <- 0
graph.max <- .35
graph.by <- .05

x.axis <- c(0:1)

pdf("CCOA_pred_real_maj.pdf", height = 7, width = 6)    #open pdf device
par(mar=c(4, 5, 4, 1.5))                            #set margins for plot, leaving lots of room on left-margin (2nd number in margin command) for variable names
plot(maj.pred.prob, maj$switch.rev, type = "n", axes = F, xlab = "", ylab = "actual switch", pch = 19,cex = .5,      
   xlim = c(0,1), ylim = c(0,1), yaxs = "i", main = "") 
points(maj.pred.prob, jitter(maj$switch.rev,factor=.1), pch = 19,cex = .1, type = "p")   
abline(lm(maj$switch.rev~maj.pred.prob))
lines(lowess(unlist(maj$switch.rev) ~ unlist(maj.pred.prob)))

title(main = "",   cex.main = .8 #,
#xlab = NULL, ylab = NULL,
 #    line = NA, outer = FALSE
 )
axis(1, at = seq(0,1,by= .1 ), labels = seq(0,1,by= .1 ), tick = T,             #draw x-axis and labels with tick marks
   cex.axis = .7, mgp = c(2,.5,0), las=3)                           #reduce label size, moves labels closer to tick marks
#it's easier to lookup coefs at top of graph
axis(2, at = seq(0,1,by=.1), labels = seq(0,1,by=.1), tick = T, cex.axis = .5, mgp = c(2,.5,0))         #draw y-axis with tick marks, make labels perpendicular to axis and closer to axis
abline(v=0, lty = 2)                            
box()
dev.off()

















######################################################################### OLD STUFF

###################### Defecting (using MQ)

maj.45 <- glm(switch.rev ~ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + justice, subset = confM.justice==1 & conf.total.M < 6, family = binomial (link = "probit"))
summary(maj.45)
### Treatment effect of distance, holding side contant
linear.hypothesis(maj.45, "IP.dist.assignee + IP.side:IP.dist.assignee")
### Treatment effect of side, holding distance contant
linear.hypothesis(maj.45, "IP.side + IP.side:IP.dist.assignee")
linear.hypothesis(maj.45, "IP.side + 2*IP.side:IP.dist.assignee")
linear.hypothesis(maj.45, "IP.side + 3*IP.side:IP.dist.assignee")

maj.45.int <- glm(switch.rev ~ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + assignee.justice*IP.marginal + assignee.justice*uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min + justice, subset = confM.justice==1 & conf.total.M < 6, family = binomial (link = "probit"))
summary(maj.45.int)
### Treatment effect of distance, holding side constant
linear.hypothesis(maj.45.int, "IP.dist.assignee + IP.side:IP.dist.assignee")
### Treatment effect of side, holding distance constant
linear.hypothesis(maj.45.int, "IP.side + IP.side:IP.dist.assignee")
linear.hypothesis(maj.45.int, "IP.side + 2*IP.side:IP.dist.assignee")
linear.hypothesis(maj.45.int, "IP.side + 3*IP.side:IP.dist.assignee")
### Treatment effect of assignment to marginal vs. foreign twin
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:IP.marginal")
### Treatment effect of assignment to uncertaun vs. foreign twin
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:uncert.justice")
### Treatment effect of assignment to freshman vs. foreign twin
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:freshman")
### Treatment effect of assignment to closer.min vs. foreign twin
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:IP.closer.min")
### Treatment effect of assignment to closer.min AND marginal vs. foreign twin
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:IP.closer.min + assignee.justice:IP.marginal")
### For marginals, assignment to self instead of other justice who is "1" MQ unit away
linear.hypothesis(maj.45.int, "assignee.justice + assignee.justice:IP.marginal - IP.side - 1*IP.dist.assignee - 1*IP.side:IP.dist.assignee")

maj.48 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + justice, subset = confM.justice==1 & conf.total.M < 9, family = binomial (link = "probit"))
summary(maj.48)
### Treatment effect of distance, holding side contant
linear.hypothesis(maj.48, "IP.dist.assignee + IP.side:IP.dist.assignee")
### Treatment effect of side, holding distance contant
linear.hypothesis(maj.48, "IP.side + IP.side:IP.dist.assignee")
linear.hypothesis(maj.48, "IP.side + 2*IP.side:IP.dist.assignee")
linear.hypothesis(maj.48, "IP.side + 3*IP.side:IP.dist.assignee")

maj.48.int <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + assignee.justice*IP.marginal + assignee.justice*uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min + justice, subset = confM.justice==1 & conf.total.M < 9, family = binomial (link = "probit"))
summary(maj.48.int)
### Treatment effect of distance, holding side constant
linear.hypothesis(maj.48.int, "IP.dist.assignee + IP.side:IP.dist.assignee")
### Treatment effect of side, holding distance constant
linear.hypothesis(maj.48.int, "IP.side + IP.side:IP.dist.assignee")
linear.hypothesis(maj.48.int, "IP.side + 2*IP.side:IP.dist.assignee")
linear.hypothesis(maj.48.int, "IP.side + 3*IP.side:IP.dist.assignee")
### Treatment effect of assignment to marginal vs. foreign twin
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:IP.marginal")
### Treatment effect of assignment to uncertaun vs. foreign twin
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:uncert.justice")
### Treatment effect of assignment to freshman vs. foreign twin
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:freshman")
### Treatment effect of assignment to closer.min vs. foreign twin
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:IP.closer.min")
### Treatment effect of assignment to closer.min AND marginal vs. foreign twin
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:IP.closer.min + assignee.justice:IP.marginal")
### For marginals, assignment to self instead of other justice who is "1" MQ unit away
linear.hypothesis(maj.48.int, "assignee.justice + assignee.justice:IP.marginal - IP.side - 1*IP.dist.assignee - 1*IP.side:IP.dist.assignee")

###################### Conforming (using MQ)

min.48 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + uncert.justice + freshman + IP.side + IP.dist.assignee + IP.closer.min + IP.side*IP.dist.assignee + justice, subset = confM.justice==0 & conf.total.M < 9, family = binomial (link = "probit"))
summary(min.48)
### Treatment effect of distance, holding side constant
linear.hypothesis(min.48, "IP.dist.assignee + IP.side:IP.dist.assignee")
### Treatment effect of side, holding distance constant
linear.hypothesis(min.48, "IP.side + IP.side:IP.dist.assignee")
linear.hypothesis(min.48, "IP.side + 2*IP.side:IP.dist.assignee")
linear.hypothesis(min.48, "IP.side + 3*IP.side:IP.dist.assignee") 

###################### Defecting (using MQ)

maj.all <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + justice, subset = confM.justice==1, family = binomial (link = "probit"))
summary(maj.all)
 
maj.all.int <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + IP.marg.ass*IP.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min + justice, subset = confM.justice==1, family = binomial (link = "probit"))
summary(maj.all.int)

##Does assignment matter to the marginal?
linear.hypothesis(maj.all.int, "assignee.justice + IP.marg.ass + IP.marginal:IP.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.all.int, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.all.int, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.all.int, "assignee.justice + assignee.justice:IP.closer.min")

maj.47.int <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + IP.marg.ass*IP.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min + justice, subset = confM.justice==1 & (conf.total.M==4 | conf.total.M==5 | conf.total.M==6 | conf.total.M==7), family = binomial (link = "probit"))
summary(maj.47.int)

##Does assignment matter to the marginal?
linear.hypothesis(maj.47.int, "assignee.justice + IP.marg.ass + IP.marginal:IP.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.47.int, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.47.int, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.47.int, "assignee.justice + assignee.justice:IP.closer.min")

maj.8.int <- glm(switch.rev ~ log(opcount1) + nyt + laws + IP.noncontig + assignee.justice + IP.marginal + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + IP.marg.ass*IP.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*IP.closer.min + justice, subset = confM.justice==1 & conf.total.M==8, family = binomial (link = "probit"))
summary(maj.8.int)

##Does assignment matter to the marginal?
linear.hypothesis(maj.8.int, "assignee.justice + IP.marg.ass + IP.marginal:IP.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.8.int, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.8.int, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.8.int, "assignee.justice + assignee.justice:IP.closer.min")

maj.9.int <- glm(switch.rev ~ log(opcount1) + nyt + laws + assignee.justice + uncert.justice + freshman + assignee.justice* uncert.justice + assignee.justice*freshman + justice, subset = confM.justice==1 & conf.total.M==9, family = binomial (link = "probit"))
summary(maj.9.int)

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.9.int, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.9.int, "assignee.justice + assignee.justice:freshman")

###################### Conforming (using MQ)

min.all <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + justice, subset = confM.justice==0, family = binomial (link = "probit"))
summary(min.all)

min.47 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + IP.noncontig + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + justice, subset = confM.justice==0 & (conf.total.M==4 | conf.total.M==5 | conf.total.M==6 | conf.total.M==7), family = binomial (link = "probit"))
summary(min.47)

min.8 <- glm(switch.rev ~ log(opcount1) + nyt + laws + IP.noncontig + uncert.justice + freshman + IP.marg.ass + IP.closer.min + IP.closer.min.ass + justice, subset = confM.justice==0 & conf.total.M==8, family = binomial (link = "probit"))
summary(min.8)

###################### Defecting (using Baum)

maj.all2 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + justice, subset = confM.justice==1, family = binomial (link = "probit"))
summary(maj.all2)
 
maj.all.int2 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + baum.marg.ass*baum.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*baum.closer.min + justice, subset = confM.justice==1, family = binomial (link = "probit"))
summary(maj.all.int2)

##Does assignment matter to the marginal?
linear.hypothesis(maj.all.int2, "assignee.justice + baum.marg.ass + baum.marginal:baum.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.all.int2, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.all.int2, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.all.int2, "assignee.justice + assignee.justice:baum.closer.min")

maj.47.int2 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + baum.marg.ass*baum.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*baum.closer.min + justice, subset = confM.justice==1 & (conf.total.M==4 | conf.total.M==5 | conf.total.M==6 | conf.total.M==7), family = binomial (link = "probit"))
summary(maj.47.int2)

##Does assignment matter to the marginal?
linear.hypothesis(maj.47.int2, "assignee.justice + baum.marg.ass + baum.marginal:baum.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.47.int2, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.47.int2, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.47.int2, "assignee.justice + assignee.justice:baum.closer.min")

maj.8.int2 <- glm(switch.rev ~ log(opcount1) + nyt + laws + baum.noncontig + assignee.justice + baum.marginal + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + baum.marg.ass*baum.marginal + assignee.justice* uncert.justice + assignee.justice*freshman + assignee.justice*baum.closer.min + justice, subset = confM.justice==1 & conf.total.M==8, family = binomial (link = "probit"))
summary(maj.8.int2)

##Does assignment matter to the marginal?
linear.hypothesis(maj.8.int2, "assignee.justice + baum.marg.ass + baum.marginal:baum.marg.ass")

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.8.int2, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.8.int2, "assignee.justice + assignee.justice:freshman")

##Does assignment matter to the closer.min justice?
linear.hypothesis(maj.8.int2, "assignee.justice + assignee.justice:baum.closer.min")

maj.9.int2 <- glm(switch.rev ~ log(opcount1) + nyt + laws + assignee.justice + uncert.justice + freshman + assignee.justice* uncert.justice + assignee.justice*freshman + justice, subset = confM.justice==1 & conf.total.M==9, family = binomial (link = "probit"))
summary(maj.9.int2)

##Does assignment matter to the uncertain justice?
linear.hypothesis(maj.9.int2, "assignee.justice + assignee.justice:uncert.justice")

##Does assignment matter to freshmen?
linear.hypothesis(maj.9.int2, "assignee.justice + assignee.justice:freshman")

###################### Conforming (using Baum)

min.all2 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + baum.noncontig + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + justice, subset = confM.justice==0, family = binomial (link = "probit"))
summary(min.all2)

min.472 <- glm(switch.rev ~ conf.total.M + log(opcount1) + nyt + laws + baum.noncontig + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + justice, subset = confM.justice==0 & (conf.total.M==4 | conf.total.M==5 | conf.total.M==6 | conf.total.M==7), family = binomial (link = "probit"))
summary(min.472)

min.82 <- glm(switch.rev ~ log(opcount1) + nyt + laws + baum.noncontig + uncert.justice + freshman + baum.marg.ass + baum.closer.min + baum.closer.min.ass + justice, subset = confM.justice==0 & conf.total.M==8, family = binomial (link = "probit"))
summary(min.82)
